Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

132
Vistas
Turning array of objects into an organized object - javascript

I'm trying to manipulate this object so it matches the corresponding count to its 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}
    ],
]

To this:

     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] }
        ]

Something I tried was this, but this just gave me an array with each count seperate.

let ob = test.reduce( ( acc, e, i) => {
    let obj = {}
    
    e.forEach((value, index) => {
        obj[value.recommendationItemIdExternal] = [value.recommendationItemRating];
    })

    acc.push(obj);
    return acc

}, [] ); 
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can easily achieve this result using 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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

A nested for loop will do it. Just iterate through the data and store the count (or b) value in another object, using the id as the key.

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);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use a combination of .reduce() and .map() array methods as in the demo below.

const newArray = data.reduce((acc, cur) => cur.map((o, i) => ({
    id: o.id,
    count: [...(acc[i] && acc[i].count || []), o.count]
})), []);

Please note that this only works if each element of the original array has the ids in the same order, otherwise a little more processing is required.

DEMO

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 );

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda