How to filter an array by property?
-
There is an arr array:
[ { id: "5jq", url: "https://cdn2.thecatapi.com/images/5jq.jpg", width: 320 }, { id: "5qj", url: "https://cdn2.thecatapi.com/images/5qj.jpg", width: 480 } ]
A similar array newArr comes. How to add objects from the newArr array to the arr array that are not in arr by id?JavaScript Marilyn McClure, May 21, 2019 -
arr.push(...newArr.filter(n => !arr.some(m => m.id === n.id)));
or
arr = Object.values([ ...arr, ...newArr ].reduce((acc, n) => (acc[n.id] ||= n, acc), {}));
Emerson Lloyd
1 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!