I have 2 different arrays I would like to merge.
var arr1 = [ { cardId: 1001, knowCnt: 0, dknowCnt: 1 }, { cardId: 1002, knowCnt: 0, dknowCnt: 2 }, { cardId: 1003, knowCnt: 1, dknowCnt: 3 } ];
var arr2= [
{
cardId: 1001,
videoId: "l4LjOvERDoM",
startSeconds: 5,
endSeconds: 10,
},
{
cardId: 1002,
videoId: "jsFVnf3iF3w",
startSeconds: 5,
endSeconds: 10,
},
{
cardId: 1003,
videoId: "dc0q0H9CO9k",
startSeconds: 5,
endSeconds: 10,
},
{
cardId: 1004,
videoId: "kdZe5ZjfNX4",
startSeconds: 5,
endSeconds: 10,
}
];
The 2 arrays have a matching cardId and I would like the knowCnt and dknowCnt in arr1 to be added to their respective cardId properties in arr2
. A potential solution from here was:
var mergeArrays = function() {
var merged = [];
const mergeById = (a1, a2) =>
a1.map(itm => ({
...a2.find((item) => (item.id === itm.id) && item),
...itm
}));
console.log(mergeById(arr1, arr2));
}
mergeArrays()
This works well but only returns the matching items. How can I return a single array with both the matching items (updated as above) but also including the remaining items from arr2. In other words the resulting array would look like arr2 but with 3 items updated from arr1? Note: the arrays may not be in matching order.
Thanks