Javascript数组对象去重

tech2024-01-18  77

var arry = [{ name: "a", value: 12 }, { name: "a", value: 23 }, { name: "b", value: 44 },{ name: "c", value: 78 },{ name: "b", value: 55 },{ name: "d", value: 88 }];

方法一:es6的Map(),去重arry

let reduceList = () => { let map = new Map(); for (let item of arry) { if (!map.has(item.name)) { map.set(item.name, item); } } return [...map.values()]; }

方法二:我们可以借助数组中reduce方法,访问遍历数组,其也是借助访问对象属性方法

let reduceList = () => { arry = arry.reduce(function(a, b) { obj[b.name] ? '' : obj[b.name] = true && a.push(b); return a; }, []) return arry; }
最新回复(0)