SOURCE

const groupBy = (objectArray, property) => objectArray.reduce(function (acc, obj) {
    const cpyAcc = { ...acc };
    if (!cpyAcc[property]) {
        cpyAcc[property] = [];
    }
    // 可以添加其他工具,如下为去重
    if (cpyAcc[property].indexOf(obj[property]) === -1) {
        cpyAcc[property].push(obj[property]);
    }
    return cpyAcc;
}, {});

const arr = [
    {
        a: 1,
        b: 1,
    }, {
        a: 2,
        b: 2,
    }, {
        a: 3,
        b: 3,
    }, {
        a: 4,
        b: 4,
    }
];

document.write(JSON.stringify(groupBy(arr, 'a')));
console 命令行工具 X clear

                    
>
console