Home > Net >  Turning array of objects into an organized object - javascript
Turning array of objects into an organized object - javascript

Time:09-17

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

}, [] ); 

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

  • Related