Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda