Seeking assistance with a problem regarding dynamic generation of multiple versions from an existing deep JavaScript object. This task involves a method with two parameters: first: the original object, second: either a single number or an array of numbers
For example:
let myObj = {
brown: {
50: '#f9f8f2',
100: '#f3f0e6',
},
singleProp: '#e6b01e',
propLvl1: {
color: '#32a852',
sub1: {
color: '#44eff2',
sub2: {
color: '#f2448d'
},
},
},
};
myFunction(myObject, [10, 30]);
The desired outcome would be:
MY-10-brown: {
50: '#(DYNAMICVALUE)f9f8f2',
100: '#(DYNAMICVALUE)f3f0e6',
},
MY-10-singleProp: '#(DYNAMICVALUE)e6b01e',
MY-10-propLvl1: {
color: '#(DYNAMICVALUE)32a852',
sub1: {
color: '#(DYNAMICVALUE)44eff2',
sub2: {
color: '#(DYNAMICVALUE)f2448d'
},
},
}
MY-30-brown: {
50: '#(DYNAMICVALUE)f9f8f2',
100: '#(DYNAMICVALUE)f3f0e6',
},
MY-30-singleProp: '#(DYNAMICVALUE)e6b01e',
MY-30-propLvl1: {
color: '#(DYNAMICVALUE)32a852',
sub1: {
color: '#(DYNAMICVALUE)44eff2',
sub2: {
color: '#(DYNAMICVALUE)f2448d'
},
},
}
Current progress made on this task includes:
export default function generateObjects(obj, numbers) {
let newObj = {};
for (let q = 0; q < transparentValue.length; q += 1) {
let Obj = doTheJob(obj, transparentValue[q]);
Object.assign(newObj, Obj);
}
return newObj;
}
function doTheJob(obj, number) {
const newObj = {};
let newKey = '';
Object.keys(obj).forEach(function (key) {
let trim = `${obj[key]}`.substring(1);
let newValue = `#${anotherObject[number]}${trim}`;
if (typeof obj[key] === 'object') {
newKey = `MY-${number}-${key}`;
newObj[newKey] = obj[key];
generateNewObj(newObj[newKey], number);
return;
}
if (typeof obj[key] === 'string') {
newObj[key] = newValue;
}
});
return newObj;
}