天天看点

ES6数组对象去重

<script>
    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',
        },
        {
            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));
</script>