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 }]];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) }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));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 } ] ] */