Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

164
Views
dynamically generated deep objects

I want to ask for help with the problem. I have an existing deep Javascript object from which I want to dynamically generate multiple versions. I have a method that has 2 parameters. first: the object from which I want to generate new ones, second: a 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]);

my goal 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'
      },
    },
  }

So far I have reached him:

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;
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could create new properties for the first level of the object and take copies of data.

const
    copy = value => typeof value === 'object'
        ? Object.fromEntries(Object.entries(value).map(([k, v]) => [k, copy(v)]))
        : typeof value === 'string'
            ? value.replace('#', '#DYNAMICVALUE')
            : value
    create = (object, values, header) => Object.fromEntries(Object
        .entries(object)
        .reduce((r, [k, v]) => [...r, ...values.map(i => [[header, i, k].join('-'), copy(v)])], [])
    ),
    myObj = { brown: { 50: '#f9f8f2', 100: '#f3f0e6' }, singleProp: '#e6b01e', propLvl1: { color: '#32a852', sub1: { color: '#44eff2', sub2: { color: '#f2448d' } } } };

console.log(create(myObj, [10, 30], 'my'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You can:

  • Create a new object

  • Loop through each number in the array

    • Inside the loop, loop through each property in the object and assign the value of the property to the new object's modified property ("MY-"+num+"-"+prop).

let myObj = {
  brown: {
    50: '#f9f8f2',
    100: '#f3f0e6',
  },
  singleProp: '#e6b01e',
  propLvl1: {
    color: '#32a852',
    sub1: {
      color: '#44eff2',
      sub2: {
        color: '#f2448d'
      },
    },
  },
};

function process(obj, numArr){
  const newObj = {};
  for(const num of numArr){
    for(const prop in obj){
      newObj['MY-'+num+'-'+prop] = obj[prop];
    }
  }
  return newObj;
}

console.log(JSON.stringify(process(myObj, [10, 30]), 0, 2))
.as-console-wrapper{max-height:100%!important;top:0}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!