There are the following arrays of objects:
const list =[
{ name: "张三", age: 18, address: "北京" },
{ name: "李四", age: 20, address: "天津" },
{ name: "王五", age: 22, address: "河北" },
{ name: "张三", age: 18, address: "北京" },
{ name: "李四", age: 20, address: "天津" },
]
Among them, Zhang San and Li Si are duplicate objects;
use the Set data structure to remove duplicate objects:
const strings = list.map((item) => JSON.stringify(item));
const removeDupList = [...new Set(strings)]; //也可以使用Array.from(new Set(strings))
const result = removeDupList.map((item) => JSON.parse(item));
// result
// [
// { name: "张三", age: 18, address: "北京" },
// { name: "李四", age: 20, address: "天津" },
// { name: "王五", age: 22, address: "河北" },
// ]
Explain why the new Set(strings) should be transformed above. Because the Set data structure is not a real array, it is similar to an array, and its member values are unique without duplication, so it can be used for deduplication. But because it is an array-like structure, it needs to be transformed into a real array to use.