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

77
Vistas
Atascado transformando una estructura de Objeto

Dado un objeto en la forma:

 { attributeA: ['valA1', 'valA2'], attributeB: ['valB1'], attributeC: ['valC1', 'valC2', 'valC3'], }

Me gustaría transformarlo en:

 { attributeA: false, valA1: false, valA2: false, attributeB: false, valB1: false, attributeC: false, valC1: false, valC2: false, valC3: false, }

No soy versátil usando el método de reduce , pero llegué a esto:

 Object.entries(attributes).reduce((p, c) => { return [].concat(p, c[0], c[1]); }, []);

Lo que devuelve una matriz de todos los elementos y sus valores como:

 [attributeA, valA1, valA2, attributeB, valB1, attributeC, valC1, valC2, valC3]

Tengo dificultades para definir correctamente mi función de reducción para devolver el resultado que necesito

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Lo mantendría simple y usaría bucles:

 const result = {}; for (const [name, array] of Object.entries(original)) { result[name] = false; // attributeA, etc. for (const value of array) { result[value] = false; } } 

 const original = { attributeA: ["valA1", "valA2"], attributeB: ["valB1"], attributeC: ["valC1", "valC2", "valC3"], }; const result = {}; for (const [name, array] of Object.entries(original)) { result[name] = false; // attributeA, etc. for (const value of array) { result[value] = false; } } console.log(result);

... pero es posible hacerlo con una combinación de Object.fromEntries , Object.entries y flatMap :

 const result = Object.fromEntries( Object.entries(original).flatMap(([key, array]) => [[key, false], ...array.map(value => [value, false])]) ); 

 const original = { attributeA: ["valA1", "valA2"], attributeB: ["valB1"], attributeC: ["valC1", "valC2", "valC3"], }; const result = Object.fromEntries( Object.entries(original).flatMap(([key, array]) => [[key, false], ...array.map(value => [value, false])]) ); console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

 // Object const obj = { attributeA: ['valA1', 'valA2'], attributeB: ['valB1'], attributeC: ['valC1', 'valC2', 'valC3'], }; // Define new object let newObj = {}; // Simple loop of object for(const el in obj) { // Put element name in new object newObj[el] = false; // Loop through el value and add them to // new object for(const item of obj[el]) newObj[item] = false; } // Test console.log(newObj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede usar Array.prototype.reduce() para hacer el objeto,
Dentro de cada iteración, cree la key e itere el value que es una matriz con Array.prototype.forEach() :

 const obj = { attributeA: ['valA1', 'valA2'], attributeB: ['valB1'], attributeC: ['valC1', 'valC2', 'valC3'], } const res = Object.entries(obj).reduce((acc, [key, value]) => { acc[key] = false; value.forEach(item => acc[item] = false); return acc; }, {}); console.log(res);

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