I have a task at hand where I need to create a new array in JavaScript that does not contain objects matching by their id field, using two arrays of objects.
The first array consists of:
[{id: 1, name: "Jo"}, {id: 2, name: "Pat"}, {id: 3, name: "Mike"}]
And the second array contains:
[{id: 1, name: "Jo"}, {id: 2, name: "Pat"}, {id: 3, name: "Mike"}, {id: 4, name: "Mitch"}, {id: 5, name: "Karl"}]
The expected result should be:
[{id: 4, name: "Mitch"}, {id: 5, name: "Karl"}]
I am aiming to achieve this using lodash for better efficiency. It's worth noting that the first array will always be smaller or equal to the second array and will only contain objects present in the second array.
My attempt so far is as follows...
let newArray = _.reject(secondArray, {'id': firstArray});
Despite trying this solution, I haven't been successful yet and require assistance with the correct syntax.
Thank you in advance for your time and support.