Tengo un objeto de artículo y cada clave es la identificación de la característica del artículo (color, tamaño o material). Sus valores también son ID del valor de la característica (rojo, azul, etc.). Quiero agrupar cada característica con sus valores (como en resultArray). Traté de usar la función de reducción pero no puedo obtener el resultado deseado. ¿Me puede dar una pista de qué métodos utilizar para obtener este resultado?
itemAttributes = { "11": [ 19,20 ], "12": [ 21, 22, 23], "13": [ 25, 26, 27 ]} arr1 =[ {title: "colors", "id": 11 }, {title: "sizes", "id": 12 }, {title: "materials", "id": 13 } ] arr2=[ {title: "red", "attribute": 11, id: 19 }, {title: "blue", "attribute": 11, id: 20 }, {title: "10x20", "attribute": 12, id: 21 }, {title: "10x30", "attribute": 12, id: 22 }, {title: "10x40", "attribute": 12, id: 23 }, {title: "10x50", "attribute": 12, id: 24 }, {title: "metals", "attribute": 13, id: 25 }, {title: "polymers", "attribute": 13, id: 26 }, {title: "ceramics", "attribute": 13, id: 27 }, ] resultArray = [ { title: colors, items: [red, blue], }, { title: sizes, items: [10x20, 10x30, 10x40], }, { title: materials, items: [metals, polymers, ceramics], }]Lo más corto que se me ocurrió fue con un map combinado con un filter interno + map . Dado que es una transformación de arr1 , se puede usar map . En el interior, estoy filtrando desde arr2 los elementos necesarios y luego transformando los elementos filtrados para devolver una matriz de solo títulos
let itemAttributes = { "11": [ 19,20 ], "12": [ 21, 22, 23], "13": [ 25, 26, 27 ]} let arr1 =[ {title: "colors", "id": 11 }, {title: "sizes", "id": 12 }, {title: "materials", "id": 13 } ] let arr2=[ {title: "red", "attribute": 11, id: 19 }, {title: "blue", "attribute": 11, id: 20 }, {title: "10x20", "attribute": 12, id: 21 }, {title: "10x30", "attribute": 12, id: 22 }, {title: "10x40", "attribute": 12, id: 23 }, {title: "10x50", "attribute": 12, id: 24 }, {title: "metals", "attribute": 13, id: 25 }, {title: "polymers", "attribute": 13, id: 26 }, {title: "ceramics", "attribute": 13, id: 27 }, ] const res = arr1.map(({title,id}) => { const attrIds = itemAttributes[id] const items = arr2.filter(({id}) => attrIds.includes(id)).map(({title}) => title) return {title,items} }) console.log(res) let itemAttributes = { "11": [ 19,20 ], "12": [ 21, 22, 23], "13": [ 25, 26, 27 ]} let arr1 =[ {title: "colors", "id": 11 }, {title: "sizes", "id": 12 }, {title: "materials", "id": 13 } ] let arr2=[ {title: "red", "attribute": 11, id: 19 }, {title: "blue", "attribute": 11, id: 20 }, {title: "10x20", "attribute": 12, id: 21 }, {title: "10x30", "attribute": 12, id: 22 }, {title: "10x40", "attribute": 12, id: 23 }, {title: "10x50", "attribute": 12, id: 24 }, {title: "metals", "attribute": 13, id: 25 }, {title: "polymers", "attribute": 13, id: 26 }, {title: "ceramics", "attribute": 13, id: 27 }, ] let resultArray = []; arr1.map((atr) => { let filters = itemAttributes[atr.id]; let toPush = { title: atr.title, items: [] }; arr2.map((item) => { if(item.attribute === atr.id && filters.includes(item.id)) toPush.items.push(item.title) }); resultArray.push(toPush); }) console.log(resultArray)