ES6 数组内对象去重

去重Set

const arr = ['张三','张三','三张三']
let set = new Set(arr); // set 自带去重
// Set { '张三', '三张三' }
console.log(set);
console.error(Array.from(set)); // [ '张三', '三张三' ]

去重reduce

let hash = {};
let config = [{
    name: 2,
    state: true,
    output: 'Y',
}, {
    name: 3,
    state: true,
    output: 'A',
}, {
    name: 5,
    state: true,
    output: 'S',
}, {
    name: 7,
    state: true,
    output: 'B',
}];

config = [...config, {
    name: 3,
    state: false,
    output: 'A',
}]
const newArr = config.reduceRight((item, next) => {
    hash[next.name] ? '' : hash[next.name] = true && item.push(next);
    return item
}, []);

console.log(JSON.stringify(newArr));

// [{"name":3,"state":false,"output":"A"},{"name":7,"state":true,"output":"B"},{"name":5,"state":true,"output":"S"},{"name":2,"state":true,"output":"Y"}]

你可能感兴趣的:(ECMAScript6)