📌  相关文章
📜  javascript 按属性减少对象数组 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:15.699000             🧑  作者: Mango

代码示例3
var cars = [
    {
        'make': 'audi',
        'model': 'r8',
        'year': '2012'
    }, {
        'make': 'audi',
        'model': 'rs5',
        'year': '2013'
    }, {
        'make': 'ford',
        'model': 'mustang',
        'year': '2012'
    }, {
        'make': 'ford',
        'model': 'fusion',
        'year': '2015'
    }, {'make': 'kia', 'model': 'optima', 'year': '2012'},
];

result = cars.reduce((h, car) => Object.assign(h, { [car.make]:( h[car.make] || [] ).concat({model: car.model, year: car.year}) }), {})

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