我想融合 Array.filter()
函数来删除重复的对象
我能够在字符串或整数数组的情况下实现。但是我无法使用对象数组实现与 names
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let x = names => names.filter((v, i, arr) => arr.indexOf(v) === i);
console.log(x(names)); //[ 'John', 'Paul', 'George', 'Ringo' ]
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" } ];
// returns the same original array
你能帮忙吗?
最佳答案
然后使用 Array#reduce()
和一个 Map
累加器将 Map 的 values()
散布到数组中
const names = [
{ name: "John" },
{ name: "Paul" },
{ name: "George" },
{ name: "Ringo" },
{ name: "John" } ];
const unique = [... names.reduce((a,c)=>(a.set(c.name,c)),new Map).values()]
console.log(unique)
关于javascript - Array.filter() 删除重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53226100/