Using Reduce | function drillDownConfig(config, ...keys) {
let result = config;
return keys.reduce((acc, key) => {
if (result && typeof result === 'object' && key in result) {
result = result[key];
}
return result;
}, result);
}
function filter(configSegment, keys) {
return keys.reduce((acc, key) => {
if (key in configSegment) {
return {
...acc,
[key]: configSegment[key],
};
}
return acc;
}, {});
}
const blockData = drillDownConfig(data, 'publication', 'publicRuntimeConfig');
const filteredData = filter(blockData, ['nav', 'componentVariants', 'publicationName', 'socialPlatforms', 'mastheadLogo']);
| ready |
using filter | function drillDownConfig(config, ...keys) {
if(keys.length === 0) {
return config;
}
const result = config[keys[0]];
if (result) {
return drillDownConfig(result, ...keys.slice(1));
}
return undefined;
}
function filter(conf, keys) {
const asArray = Object.entries(conf);
const filtered = asArray.filter(([key, value]) => keys.includes(key) && value !== undefined);
return Object.fromEntries(filtered);
}
const blockData = drillDownConfig(data, 'publication', 'publicRuntimeConfig');
const filteredData = filter(blockData, ['nav', 'componentVariants', 'publicationName', 'socialPlatforms', 'mastheadLogo']);
| ready |
Combination - Recursive & Reduce | function drillDownConfig(config, ...keys) {
if(keys.length === 0) {
return config;
}
const result = config[keys[0]];
if (result) {
return drillDownConfig(result, ...keys.slice(1));
}
return undefined;
}
function filter(configSegment, keys) {
return keys.reduce((acc, key) => {
if (key in configSegment) {
return {
...acc,
[key]: configSegment[key],
};
}
return acc;
}, {});
}
const blockData = drillDownConfig(data, 'publication', 'publicRuntimeConfig');
const filteredData = filter(blockData, ['nav', 'componentVariants', 'publicationName', 'socialPlatforms', 'mastheadLogo']);
| ready |
Combination Reduce and filter | function drillDownConfig(config, ...keys) {
let result = config;
return keys.reduce((acc, key) => {
if (result && typeof result === 'object' && key in result) {
result = result[key];
}
return result;
}, result);
}
function filter(conf, keys) {
const asArray = Object.entries(conf);
const filtered = asArray.filter(([key, value]) => keys.includes(key) && value !== undefined);
return Object.fromEntries(filtered);
}
const blockData = drillDownConfig(data, 'publication', 'publicRuntimeConfig');
const filteredData = filter(blockData, ['nav', 'componentVariants', 'publicationName', 'socialPlatforms', 'mastheadLogo']);
| ready |
All in 1 function | const builder = (data, sections) => {
sections.reduce((acc, { segment, runtime, keys }) => {
const sectionData = data[segment][runtime];
if (!sectionData) return acc;
if (keys === 'all' || keys.length === 0) {
acc[segment] = sectionData;
return {
...acc,
[segment]: sectionData,
};
}
acc[segment] = keys.reduce((sectionAcc, key) => {
if (sectionData[key] !== undefined) {
return {
...sectionAcc,
[key]: sectionData[key],
};
}
return sectionAcc;
}, {});
return acc;
}, {});
};
builder(data, [{segment: 'publication', runtime: 'publicRuntimeConfig', keys: ['nav', 'componentVariants', 'publicationName', 'socialPlatforms', 'mastheadLogo']}]);
| ready |