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

200
Views
Cómo crear varias matrices de objetos a partir de una única matriz de objetos en función del valor de la propiedad

Si tengo una matriz de objetos como este, quiero agruparlos por valor de propiedad

 const unitsPhotos = [{ fieldname: 'unit_1', name: 'Photo 1', size: 324 }, { fieldname: 'unit_2', name: 'Photo 11', size: 321 }, { fieldname: 'unit_1', name: 'Photo 41', size: 541 }, { fieldname: 'unit_2', name: 'Photo 14', size: 123 }, { fieldname: 'unit_3', name: 'Photo 144', size: 1223 } ];

Cómo crear tres nuevas matrices separadas de objetos basadas en el nombre del campo (o una matriz única que contiene esas matrices de objetos) algo como esto

 const groupedArray = [ [{ fieldname: 'unit_1', name: 'Photo 1', size: 324 }, { fieldname: 'unit_1', name: 'Photo 132', size: 325 }], [{ fieldname: 'unit_2', name: 'Photo 11', size: 321 }, { fieldname: 'unit_2', name: 'Photo 14', size: 123 }], [{ fieldname: 'unit_3', name: 'Photo 144', size: 1223 }]];
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

primero puede hacer única la matriz y luego empujarla a groupedArray de esta manera:

 const unitsPhotos = [{ fieldname: 'unit_1', name: 'Photo 1', size: 324 }, { fieldname: 'unit_2', name: 'Photo 11', size: 321 }, { fieldname: 'unit_1', name: 'Photo 41', size: 541 }, { fieldname: 'unit_2', name: 'Photo 14', size: 123 }, { fieldname: 'unit_3', name: 'Photo 144', size: 1223 } ]; const unitsPhotosArr = unitsPhotos.map(item => item.fieldname) // you can find uniques with this function const toFindUniques = arry => arry.filter((item, index) => arry.indexOf(item) === index) const uniques = toFindUniques(unitsPhotosArr); const groupedArray = []; for (const element of uniques) { const item = unitsPhotos.filter(el => el.fieldname === element); groupedArray.push(item) }

about 4 years ago · Juan Pablo Isaza Report

0

Hay muchos enfoques diferentes para hacer esto, aunque recomendaría usar Array.prototype.reduce para evitar tener que iterar sobre la lista muchas veces cuando no es necesario. Para hacer esto, creé un objeto, luego presioné cada elemento en una clave del nombre del campo y creé la matriz si no está presente. Aquí está el código:

 const unitsPhotos = [{ fieldname: 'unit_1', name: 'Photo 1', size: 324 }, { fieldname: 'unit_2', name: 'Photo 11', size: 321 }, { fieldname: 'unit_1', name: 'Photo 41', size: 541 }, { fieldname: 'unit_2', name: 'Photo 14', size: 123 }, { fieldname: 'unit_3', name: 'Photo 144', size: 1223 } ]; function groupPhotos(photos) { return Object.values(photos.reduce((acc, cur) => { acc[cur.fieldname] ??= []; acc[cur.fieldname].push(cur); return acc; }, {})); } console.log(groupPhotos(unitsPhotos));

Vínculo de juegos de TypeScript

about 4 years ago · Juan Pablo Isaza Report

0

Usa Array#reduce() para producir un objeto:

 const unitsPhotos = [{fieldname: 'unit_1', name: 'Photo 1', size: 324 }, { fieldname: 'unit_2', name: 'Photo 11', size: 321 }, { fieldname: 'unit_1', name: 'Photo 41', size: 541 }, { fieldname: 'unit_2', name: 'Photo 14', size: 123 }, { fieldname: 'unit_3', name: 'Photo 144', size: 1223 }]; const groupedArray = unitsPhotos.reduce((prev, cur) => ({...prev,[cur.fieldname]:(prev[cur.fieldname] || []).concat(cur)}),{} ); console.log( groupedArray ); //You can leave it as an object OR //if it has to be an array use Object.values() //Like so: Output is same as your desired output. console.log( Object.values(groupedArray) ); /*OUTPUT: [ [ { "fieldname": "unit_1", "name": "Photo 1", "size": 324 }, { "fieldname": "unit_1", "name": "Photo 41", "size": 541 } ], [ { "fieldname": "unit_2", "name": "Photo 11", "size": 321 }, { "fieldname": "unit_2", "name": "Photo 14", "size": 123 } ], [ { "fieldname": "unit_3", "name": "Photo 144", "size": 1223 } ] ] */

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!