In the course of daily development, whether it’s data returned to us by an interface or data we create ourselves, we inevitably encounter cases where there are duplicates.
Two simple methods of object array de-duplication are described below.
Suppose we now have this array of arr objects as follows.
-
de-duplication through properties and methods of Es6 new Set() instances
-
Using the reduce method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
const hash = {}; const newArr = arr.reduce((pre, next) => { if (!hash[next.id]) { hash[next.id] = pre.push(next); } return pre; }, []); console.log('newArr: ', newArr); // newArr: // [ // { // "id": "1", // "pId": "0", // "title": "A" // }, // { // "id": "2", // "pId": "0", // "title": "B" // }, // { // "id": "3", // "pId": "0", // "title": "C" // } // ]
For normal array de-duplication, you can use ES6’s Array.from
.