Home > Mobile >  Add all objects with the same id from one array into an array of an object with the same id in anoth
Add all objects with the same id from one array into an array of an object with the same id in anoth

Time:04-26

I have 2 arrays. I need to move all the objects that have the same id in arraySecondary into an array inside an object with the same id in arrayPrimary.

Example:

    const arrayPrimary = [
      { "id": "1", "location": "France", "price": "12,3" },
      { "id": "2", "location": "Germany", "price": "12,0" },
      { "id": "3", "location": "USA", "price": "10" },
      { "id": "4", "location": "Italy", "price": "16" },
    ];

    const arraySecondary = [
      { "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" },
      { "id": "2", "location": "Paris", "dd": "lql" },
      { "id": "3", "location": "Egypt" },
      { "id": "2", "name": "joe", "location": "London" },
      { "id": "1", "location": "location", "name": "april" },
      { "id": "2", "name": "mei", "location": "Barcelona" },
    ];

Expected result:

    [
      {
        id: 1,
        location: "France",
        price: "12,3",
        area: [
          { id: 1, location: "location", name: "april" },
          { id: 1, name: "phil", location: "New York", price: "1,3", dd: "lql" },
        ],
      },
      {
        id: 2,
        location: "Germany",
        price: "12,0",
        area: [
          { id: 2, location: "Paris", dd: "lql" },
          { id: 2, name: "joe", location: "London" },
          { id: 2, name: "mei", location: "Barcelona" },
        ],
      },
      { id: 3, location: "USA", price: 10, area: [{ id: 3, location: "Egypt" }] },
      { id: 4, location: "Italy", price: 16 },
    ];
    //or json

First i add an empty array to each object in arrayPrimary.

 arrayPrimary.map((v) => ({ ...v, area: [] }));

After that i filter arraySecondary by id and push all the results into area array in each object in arrayPrimary. But here i get stuck.

    console.log(
      arrayPrimary.forEach((main) =>
        main.area.push(arraySecondary.filter((items) => items.id === main.id))
      )
    );

Second idea is to first order each object in arraySecondary by id and then push that into empty area array in arrayPrimary

let op = arrayItems.reduce((op,inp) => {
  op[inp.id] = op[inp.id] || []
  op[inp.id].push(inp)
  return op
},{})

console.log(op)

And this is where i am stuck with both ideas.

CodePudding user response:

something like this?

const arrayPrimary = [
      { "id": "1", "location": "France", "price": "12,3" },
      { "id": "2", "location": "Germany", "price": "12,0" },
      { "id": "3", "location": "USA", "price": "10" },
      { "id": "4", "location": "Italy", "price": "16" },
    ];

    const arraySecondary = [
      { "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" },
      { "id": "2", "location": "Paris", "dd": "lql" },
      { "id": "3", "location": "Egypt" },
      { "id": "2", "name": "joe", "location": "London" },
      { "id": "1", "location": "location", "name": "april" },
      { "id": "2", "name": "mei", "location": "Barcelona" },
    ];
    
const composed = arrayPrimary.map(d => {
  return {
    ...d,
    area: arraySecondary.filter(({id}) => d.id === id)
  }
})

console.log(composed)

CodePudding user response:

You can do it as followed :

const arrayResut=arrayPrimary.map(primaryElement=>{
   primaryElement.area=arraySecondary.filter(secondaryElement=>secondaryElement.id==primaryElement.id);
   return primaryElement;
})

This will create a copy of arrayPrimary, and in the process add an area key which will contain all the elements of arraySecondary if the id of the arraySecondary element is equal to the id of the arrayPrimary element.

  • Related