Estoy tratando de manipular este objeto para que coincida con el recuento correspondiente a su id .
const data= [ [ {id: "333", count: 555}, {id: "2", count: 133}, {id: "444", count: 13}, {id: "433", count: 32}, {id: "3333", count: 2} ], [ {id: "333", count: 5565}, {id: "2", count: 1633}, {id: "444", count: 136}, {id: "433", count: 362}, {id: "3333", count: 62} ], [ {id: "333", count: 585}, {id: "2", count: 1383}, {id: "444", count: 138}, {id: "433", count: 328}, {id: "3333", count: 82} ], ]A esto:
const newData = [ { id: "333", count: [555, 5565, 585] }, { id: "2", count: [133, 1633, 1383] }, { id: "444", count: [13, 136, 138] }, { id: "433", count: [32, 362, 328] }, { id: "3333", count: [2, 62, 82] } ]Algo que probé fue esto, pero esto solo me dio una matriz con cada conteo por separado.
let ob = test.reduce( ( acc, e, i) => { let obj = {} e.forEach((value, index) => { obj[value.recommendationItemIdExternal] = [value.recommendationItemRating]; }) acc.push(obj); return acc }, [] );Puede lograr fácilmente este resultado usando Map and reduce
const data = [ [ { id: "333", count: 555 }, { id: "2", count: 133 }, { id: "444", count: 13 }, { id: "433", count: 32 }, { id: "3333", count: 2 }, ], [ { id: "333", count: 5565 }, { id: "2", count: 1633 }, { id: "444", count: 136 }, { id: "433", count: 362 }, { id: "3333", count: 62 }, ], [ { id: "333", count: 585 }, { id: "2", count: 1383 }, { id: "444", count: 138 }, { id: "433", count: 328 }, { id: "3333", count: 82 }, ], ]; const map = new Map(), newData = []; const dict = data.flat().reduce((acc, curr) => { !acc.has(curr.id) ? map.set(curr.id, [curr.count]) : map.get(curr.id).push(curr.count); return acc; }, map); for (let [id, count] of dict) { newData.push({ id, count }); } console.log(newData);Un bucle for anidado lo hará. Simplemente itere a través de los datos y almacene el valor de conteo (o b) en otro objeto, usando la identificación como clave.
let adjusted = {}; data.forEach(e => { for (let item of e) { if (adjusted[item.id] == undefined) adjusted[item.id] = []; if(item.count != undefined) adjusted[item.id].push(item.count); else if(item.b != undefined) adjusted[item.id].push(item.b); } }); console.log(adjusted); const data = [ [{ id: "333", count: 555 }, { id: "2", count: 133 }, { id: "444", count: 13 }, { id: "433", count: 32 }, { id: "3333", count: 2 }, ], [{ id: "333", count: 5565 }, { id: "2", count: 1633 }, { id: "444", count: 136 }, { id: "433", count: 362 }, { id: "3333", count: 62 }, ], [{ id: "333", b: 585 }, { id: "2", b: 1383 }, { id: "444", b: 138 }, { id: "433", b: 328 }, { id: "3333", b: 82 }, ], ] let adjusted = {}; data.forEach(e => { for (let item of e) { if (adjusted[item.id] == undefined) adjusted[item.id] = []; if(item.count != undefined) adjusted[item.id].push(item.count); else if(item.b != undefined) adjusted[item.id].push(item.b); } }); console.log(adjusted);Puede usar una combinación de métodos de .reduce() y .map() como en la demostración a continuación.
const newArray = data.reduce((acc, cur) => cur.map((o, i) => ({ id: o.id, count: [...(acc[i] && acc[i].count || []), o.count] })), []); Tenga en cuenta que esto solo funciona si cada elemento de la matriz original tiene los id en el mismo orden; de lo contrario, se requiere un poco más de procesamiento.
MANIFESTACIÓN
const data= [ [ {id: "333", count: 555}, {id: "2", count: 133}, {id: "444", count: 13}, {id: "433", count: 32}, {id: "3333", count: 2} ], [ {id: "333", count: 5565}, {id: "2", count: 1633}, {id: "444", count: 136}, {id: "433", count: 362}, {id: "3333", count: 62} ], [ {id: "333", count: 585}, {id: "2", count: 1383}, {id: "444", count: 138}, {id: "433", count: 328}, {id: "3333", count: 82} ], ]; const newArray = data.reduce((acc,cur) => cur.map((o,i) => ({id:o.id,count:[...(acc[i] && acc[i].count || []),o.count]})),[]); console.log( newArray );