Skip to content
Advertisement

create new array from nested array object

i want to create a new array from api, but i don’t know how to make it, i’m very confused in looping each array

This is each data

 const group_one = [
      {
        name: "smash",
        id: "012112"
      },
      {
        name: "ahlan wa sahlan",
        id: "123123"
      },
      {
        name: "ahh",
        id: "1231239"
      },
      {
        name: "laki",
        id: "21312"
      }
    ];
    
    const group_two = [
      {
        name: "ahh",
        id: "1231239"
      },
      {
        name: "laki",
        id: "21312"
      }
    ];
    
    const group_three = [
      {
        name: "smash",
        id: "012112"
      },
      {
        name: "ahlan wa sahlan",
        id: "123123"
      }
    ];
    

this is the main data of api

const data = [
          {
            body: group_one,
            group_id: "01"
          },
          {
            body: grouop_two,
            group_id: "02"
          },
          {
            body: group_three,
            group_id: "03"
          }
        ];
    export default data;

i want to create a new array like this, bcs i want to create a new object containing the group_id of each same data in the array

const newArray = [
{
    name: "smash",
    id: "012112",
group_id: ["01","03"]
  },
  {
    name: "ahlan wa sahlan",
    id: "123123",
group_id: ["01","03"]
  },
  {
    name: "ahh",
    id: "1231239",
group_id: ["01","02"]
  },
  {
    name: "laki",
    id: "21312",
group_id: ["01","02"]
  }
];

can someone help me? with articles or codes. thanks for helping me (sry for my bad english)

Advertisement

Answer

Please see below commented code:

const group01 = [
    {
        name: 'smash',
        id: '012112'
    },
    {
        name: 'ahlan wa sahlan',
        id: '123123'
    },
    {
        name: 'ahh',
        id: '1231239'
    },
    {
        name: 'laki',
        id: '21312'
    }
];

const group02 = [
    {
        name: 'ahh',
        id: '1231239'
    },
    {
        name: 'laki',
        id: '21312'
    }
];

const group03 = [
    {
        name: 'smash',
        id: '012112'
    },
    {
        name: 'ahlan wa sahlan',
        id: '123123'
    }
];

const data = [
    {
        body: group01,
        group_id: '01'
    },
    {
        body: group02,
        group_id: '02'
    },
    {
        body: group03,
        group_id: '03'
    }
];

function regroup(input) {
    // USE Map FOR EASIER ITEM HANDLING.
    const output = new Map();

    // LOOP MAIN DATA ARRAY.
    input.forEach(({body, group_id}) => {
        // LOOP EACH GROUP.
        body.forEach(({name, id}) => {
            // USE id TO GET AN ITEM FROM output OR CREATE A NEW ONE IF IT DOES NOT EXIST.
            const item = output.get(id) || {name, id, group_id: []};
            // PUSH CURRENT group_id TO THE RESPECTIVE ARRAY.
            item.group_id.push(group_id);
            // SAVE ITEM TO OUTPUT Map AGAIN.
            output.set(id, item);
        });
    });

    // RETURN OUTPUT.
    return Array.from(output.values());
}

const new_data = regroup(data);

console.log(new_data);
Advertisement