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

79
Views
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 answers
Answer question

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 Report

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 Report

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 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!