Using underscore groupby to group an array of cars by their colour

前端 未结 3 1067
梦如初夏
梦如初夏 2020-12-31 06:28

I have an array of cars.

car = {
    make: \"nissan\",
    model: \"sunny\",
    colour: \"red\"
};

How would I use underscore.js to grou

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-31 06:35

    var data = [
        {
            "name": "jim",
            "color": "blue",
            "age": "22"
        },
        {
            "name": "Sam",
            "color": "blue",
            "age": "33"
        },
        {
            "name": "eddie",
            "color": "green",
            "age": "77"
        },
        {
            "name": "Dheeraj",
            "color": "blue",
            "age": "25"
        },
        {
            "name": "Suraj",
            "color": "green",
            "age": "25"
        }
    ];
    
    var result = _.groupBy(data,"color");
    console.log(result);
    

提交回复
热议问题