Home > Software design >  Modify an array of objects based on id & code and store rest data inside a nested array object
Modify an array of objects based on id & code and store rest data inside a nested array object

Time:10-10

I have an array objects as shown below -

var inputArray=[
  {
    id: 86317,
    code: 'ee78',
    G1: 0,
    G2: 1,
    P1: 0,
    P3: 0
  },
  {
    id: 71989,
    code: '703a',
    G1: 2
  },
  {
    id: 28285,
    code: 're53',
    G1: 1
  },
  { id: 35022, code: 'cdv3', G2: 3 },
  {
    id: 86317,
    code: 'uty4',
    G1: 2,
    G2: 1,
    P1: 0,
    P3: 2
  }

]

My question is how to insert the objects data with similar id into a sub array called data and inside data array there will be objects (constists of code,G1,G2,P1,P2 etc) which had same parent id . Output is shown below-

var outputArray=[
 {
   id: 86317,
   data: [
    {  
      code: 'ee78',
      G1: 0,
      G2: 1,
      P1: 0,
      P3: 0
    },
    {
      code: 'uty4',
      G1: 2,
      G2: 1,
      P1: 0,
      P3: 2
    },
   ],
 },
{
  id: 71989,
  data: [
    {
      code: '703a',
      G1: 2
    }]
  },
  {
  id: 28285,
data: [{
    code: 're53',
    G1: 1
  }]
},
{
id: 35022,
data: [{
    code: 'cdv3',
    G2: 3
  }]
 }
]

Code -

var newarr = []
var outputArray = inputArray.forEach((ele) => {
let m = newarr.find(e => { return e.id === ele.id })

if (!m) {
m = {
  id: ele.id,
  data:[
    {code:ele.code,
    G1:ele.G1,
    G2:ele.G2,
    P1:ele.P1,
    P3:ele.P3}
    ]
  }
}
 newarr.push(m)
})

CodePudding user response:

Can try with below code

var inputArray=[
  {
    id: 86317,
    code: 'ee78',
    G1: 0,
    G2: 1,
    P1: 0,
    P3: 0
  },
  {
    id: 71989,
    code: '703a',
    G1: 2
  },
  {
    id: 28285,
    code: 're53',
    G1: 1
  },
  { id: 35022, code: 'cdv3', G2: 3 },
  {
    id: 86317,
    code: 'uty4',
    G1: 2,
    G2: 1,
    P1: 0,
    P3: 2
  }
]

var uniqueIds= [...new Set(inputArray.map(val=>val.id))]

var outputArray = uniqueIds.map(id=>{
  return {id:id,data:inputArray.filter(obj=>{
    if(obj.id ===id)
      {
        delete obj.id;
        return obj;
      }
      })}
})

console.log(outputArray)

CodePudding user response:

You can use loadash to achieve this in a matter of few lines. Please find the following working link to the solution: Code sandbox link

Also here is the code to acheive the same

import groupBy from "lodash/groupBy";
import map from "lodash/map";
const sample = [
  {
    id: 86317,
    code: "ee78",
    G1: 0,
    G2: 1,
    P1: 0,
    P3: 0
  },
  {
    id: 71989,
    code: "703a",
    G1: 2
  },
  {
    id: 28285,
    code: "re53",
    G1: 1
  },
  { id: 35022, code: "cdv3", G2: 3 },
  {
    id: 86317,
    code: "uty4",
    G1: 2,
    G2: 1,
    P1: 0,
    P3: 2
  }
];
const result = map(groupBy(sample, "id"), (item) => {
  const obj = {
    id: item[0].id,
    data: []
  };
  item.forEach((data) => {
    delete data?.id;
    obj.data.push(data);
  });
  return obj;
});
console.log(result);
  • Related