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

93
Views
Cómo convertir objetos en una matriz con las mismas claves en un objeto separado propio en la misma matriz

Tengo una matriz de objetos con las mismas claves, quiero crear una nueva matriz de objetos que cree un nuevo objeto que combine los únicos en uno.

 const arrayOfObjects = [ { 'Serial Number': '90946117350061093222581604839920' }, { 'Serial Number': '77362117350061093222581604891733' }, { 'Serial Number': '77362117350061093222581604891734' }, { 'Part Number': 'TR40-60C779-AA' }, { 'Part Number': 'ST41-60C780-AA' }, { 'Part Number': 'QT41-60C780-AA' }, { 'Cell Count': '28' }, { 'Cell Count': '27' }, { 'Cell Count': '29' }, { 'Length': '10' }, { 'Length': '11' }, { 'Length': '11' }, ]

Por lo tanto, debe verse como el siguiente.

 const desiredArrayOfObjects = [ { 'Serial Number': '90946117350061093222581604839920', 'Part Number': 'TR40-60C779-AA', 'Cell Count': '27', 'Length': '10', }, { 'Serial Number': '77362117350061093222581604891733', 'Part Number': 'ST41-60C780-AA', 'Cell Count': '28', 'Length': '11', }, { 'Serial Number': '77362117350061093222581604891734', 'Part Number': 'QT41-60C780-AA', 'Cell Count': '29', 'Length': '12', }, ]

Lo he intentado con el siguiente código, pero claramente no lo estoy haciendo bien, la ayuda es bienvenida y apreciada.

 let newArr = []; arrayOfObjects.map((x,i) => { Object.entries(x).map(([key, value]) => { if(!newArr.length){ if(Object.keys(newArr[i]).toString() === key){ newArr.push({[key]: value}) } } }) })
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Creo que es mejor si primero los separas y luego los vuelves a armar.

 const arrayOfObjects = [ { 'Serial Number': '90946117350061093222581604839920' }, { 'Serial Number': '77362117350061093222581604891733' }, { 'Serial Number': '77362117350061093222581604891734' }, { 'Part Number': 'TR40-60C779-AA' }, { 'Part Number': 'ST41-60C780-AA' }, { 'Part Number': 'QT41-60C780-AA' }, { 'Cell Count': '28' }, { 'Cell Count': '27' }, { 'Cell Count': '29' }, { 'Length': '10' }, { 'Length': '11' }, { 'Length': '11' }, ] const keys = ['Serial Number', 'Part Number', 'Cell Count', 'Length'] const separated = arrayOfObjects.reduce((res, item) => { const key = Object.keys(item)[0] const existing = res[key] || [] res[key] = [...existing, item[key]] return res }, {}) const result = separated[keys[0]].map((_, i) => { return keys.reduce((res, k ) => { return {...res, [k]: separated[k][i]} }, {}) }) console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Una solución que no requiere conocer las propiedades con anticipación sería separar la matriz en sub-matrices de propiedad coincidente y luego mapear sobre la más larga de estas sub-matrices fusionando los objetos contenidos en uno. (Esto requiere que todas las propiedades similares sean secuenciales en la matriz de origen).

 const arrayOfObjects = [{ 'Serial Number': '90946117350061093222581604839920' }, { 'Serial Number': '77362117350061093222581604891733' }, { 'Serial Number': '77362117350061093222581604891734' }, { 'Part Number': 'TR40-60C779-AA' }, { 'Part Number': 'ST41-60C780-AA' }, { 'Part Number': 'QT41-60C780-AA' }, { 'Cell Count': '28' }, { 'Cell Count': '27' }, { 'Cell Count': '29' }, { 'Length': '10' }, { 'Length': '11' }, { 'Length': '11' },]; const grouped = []; let prev; for (const obj of arrayOfObjects) { const [key] = Object.keys(obj); if (key !== prev) { grouped.push([]); } grouped.at(-1).push(obj); prev = key; } const result = grouped .sort(({ length: a }, { length: b }) => b - a)[0] .map((_, i) => Object.assign(...grouped.map(p => p[i] ?? {}))); console.log(result)

O bien, usar la agrupación si se desconoce el orden de la matriz de origen (el objeto en el que terminan las propiedades individuales seguirá siendo sensible al orden de la matriz de origen)

 const arrayOfObjects = [{ 'Serial Number': '90946117350061093222581604839920' }, { 'Serial Number': '77362117350061093222581604891733' }, { 'Serial Number': '77362117350061093222581604891734' }, { 'Part Number': 'TR40-60C779-AA' }, { 'Part Number': 'ST41-60C780-AA' }, { 'Part Number': 'QT41-60C780-AA' }, { 'Cell Count': '28' }, { 'Cell Count': '27' }, { 'Cell Count': '29' }, { 'Length': '10' }, { 'Length': '11' }, { 'Length': '11' },]; const grouped = {} for (const obj of arrayOfObjects) { const [[key, value]] = Object.entries(obj); (grouped[key] ??= []).push(value); } const result = []; for (const [key, values] of Object.entries(grouped)) { for (const [i, value] of values.entries()) { (result[i] ??= {})[key] = value; } } console.log(result)

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!