ItsMeAndrea
ItsMeAndrea

Reputation: 75

Is there a way to "merge" two object that are inside an array?

I have two arrays that i want two merge, something like this:

const arr1 = [{
    id: 1,
    isAvailable: true
  },
  {
    id: 2,
    isAvailable: true
  },
  {
    id: 4,
    isAvailable: true
  },
  {
    id: 6,
    isAvailable: false
  }
]

const arr2 = [{
    id: 1,
    isAvailable: false
  },
  {
    id: 2,
    isAvailable: false
  },
  {
    id: 6,
    isAvailable: false
  }
]

The outcome that I'm looking for is somethig like:

const arr3 = [{
    id: 1,
    isAvailable: false
  },
  {
    id: 2,
    isAvailable: false
  },
  {
    id: 4,
    isAvailable: true
  },
  {
    id: 6,
    isAvailable: false
  }
]

I need to update the values of the first array with the values of the second array so I can have a new array with what's truly available and what's not.

Upvotes: 2

Views: 72

Answers (4)

StepUp
StepUp

Reputation: 38094

You can filter arr1 based on arr2 and then just merge filtered arr2 with arr1

const arr1 = [{
  id: 1,  isAvailable: true
},
{
  id: 2,  isAvailable: true
},
{
  id: 4,  isAvailable: true
},
{
  id: 6,  isAvailable: false
}
];

const arr2 = [{
  id: 1,  isAvailable: false
},
{
  id: 2,  isAvailable: false
},
{
  id: 6,  isAvailable: false
}
];

const result = arr1.filter(a => !arr2.some(s => s.id == a.id)).concat(arr2);
console.log(result);

Or using map, some and find methods:

const arr1 = [{
  id: 1, isAvailable: true
},
{
  id: 2, isAvailable: true
},
{
  id: 4, isAvailable: true
},
{
  id: 6,
  isAvailable: false
}
];

const arr2 = [{
  id: 1, isAvailable: false
},
{
  id: 2, isAvailable: false
},
{
  id: 6, isAvailable: false
}
];
const result = arr1.map(a=> arr2.some(f=> f.id == a.id) ? 
                            arr2.find(f=> f.id == a.id) : a 
                        );
console.log(result);

Upvotes: 0

kapil pandey
kapil pandey

Reputation: 1903

Here is my sample program according to your example. Hope it solves your problem

const arr1 = [{
    id: 1,
    isAvailable: true
  },
  {
    id: 2,
    isAvailable: true
  },
  {
    id: 4,
    isAvailable: true
  },
  {
    id: 6,
    isAvailable: true
  }
]

const arr2 = [{
    id: 1,
    isAvailable: false
  },
  {
    id: 2,
    isAvailable: false
  },
  {
    id: 6,
    isAvailable: false
  }
]
const arr3 = []
for (let i = 0; i < arr1.length; i++) {
  let found = arr2.find(item => item.id === arr1[i].id)
  if (found)
    arr3.push(found)
  else
    arr3.push(arr1[i])
}
console.log(arr3)

Output is
[ { id: 1, isAvailable: false }, { id: 2, isAvailable: false }, { id: 4, isAvailable: true }, { id: 6, isAvailable: false } ]

Upvotes: 1

user8122577
user8122577

Reputation:

You can do this with Array.map() by overwriting the value if it exists in the second array.

const newArray = arr1.map(({ id, isAvailable}) => {
  const matchingObjects = arr2.filter(({ id: id2 }) => id === id2);
  return matchingObjects.length > 0
    ? { id, isAvailable: matchingObjects[0].isAvailable } // replace the original value
    : { id, isAvailable } // do not overwrite this value
})

Upvotes: 0

ibrahim mahrir
ibrahim mahrir

Reputation: 31682

You could first transform arr2 into a map object that maps the IDs to their availability, something like { "1": true, "4": false, ... }. And then map arr1 objects into a new array of objects while updating the isAvailable property using the map object:

let map = arr2.reduce((acc, o) => (acc[o.id] = o.isAvailable, acc), {});

let arr3 = arr1.map(o => ({
  id: o.id,
  isAvailable: map.hasOwnProperty(o.id) ? map[o.id] : o.isAvailable
}));

Each new object will have the same id as the original. Its isAvailable property will either be the value from the map object or its original value depending on whether or not it has an entry in the map object (whether or not it has an object in arr2)

Upvotes: 2

Related Questions