You could take a Set with all id from arr1 and filter arr2.
This approach
result = arr2.filter(
(ids => ({ id }) => !ids.has(id))
(arr1.reduce((s, { id }) => s.add(id), new Set))
);
utilize a closure with an IIFE (immediately-invoked function expression) over an instance of a Set with
arr1.reduce((s, { id }) => s.add(id), new Set)
and this is the value for ids
(ids => callback)(instanceOfSet)
The callback consist only a destructuring for id and a check with Set#has
({ id }) => !ids.has(id)
const
arr1 = [{ id: 1, name: 'qwerty' }, { id: 2, name: 'qwerty' }],
arr2 = [{ id: 1, name: 'qwerty' }, { id: 2, name: 'qwerty' }, { id: 3, name: 'qwerty' }, { id: 4, name: 'qwerty' }],
result = arr2.filter(
(ids => ({ id }) => !ids.has(id))
(arr1.reduce((s, { id }) => s.add(id), new Set))
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
site:stackoverflow.com js array of objects to array of ids) and remove object from js array knowing its Id (first Google result forsite:stackoverflow.com js remove array elements by id). Then useArraymethods.