Javascript 使用下划线 groupby 按颜色对一组汽车进行分组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11582284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:24:12  来源:igfitidea点击:

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

javascriptiteratorunderscore.js

提问by Jon Wells

I have an array of cars.

我有很多车。

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

How would I use underscore.js to group the array by colour?

我将如何使用 underscore.js 按颜色对数组进行分组?

I've tried a few combos but I'm not really sure how to specify my iterator condition:

我尝试了一些组合,但我不确定如何指定我的迭代器条件:

var carsGroupedByColor = _.groupBy(cars, false, colour);
var carsGroupedByColor = _.groupBy(vars, false, function(cars){ return cars[colour]; };

They all return everything in the array each time.

它们每次都返回数组中的所有内容。

回答by jabclab

You don't need the falsesecond argument, the following will work:

您不需要false第二个参数,以下将起作用:

var redCars = _.groupBy(cars, 'colour');

Note that the second parameter can either be a functionor a string. If it's a stringUnderscore groups by that property name.

请注意,第二个参数可以是 afunction或 a string。如果它是string该属性名称的下划线组。

Taken from the docs:

取自文档:

Splits a collection into sets, grouped by the result of running each value through iterator. If iteratoris a string instead of a function, groups by the property named by iteratoron each of the values.

将集合拆分为多个集合,按通过迭代器运行每个值的结果分组。如果迭代器是字符串而不是函数,则按迭代器命名的属性对每个值进行分组。

Here's a working example.

这是一个工作示例

回答by Jon Taylor

I've never used underscore js but would it not be as per their docs

我从来没有使用过下划线 js 但它不会按照他们的文档

var groupedCars = _.groupBy(cars, function(car) { return car.make; });

In fact I believe this ir more correct since it states that if the iterator is a string instead it groups by the property in the object with that string name.

事实上,我认为这更正确,因为它指出如果迭代器是一个字符串,它会根据对象中具有该字符串名称的属性进行分组。

var groupedCars = _.groupBy(cars, "make");

If you then want just the red cars (even though you really should be using a filter I guess) then you can do the following

如果您只想要红色汽车(即使您确实应该使用过滤器,我猜)那么您可以执行以下操作

var redCars = groupedCars["red"];

To use a filter instead

改用过滤器

Looks through each value in the list, returning an array of all the values that pass a truth test (iterator). Delegates to the native filter method, if it exists.

查看列表中的每个值,返回所有通过真值测试(迭代器)的值的数组。委托给本机过滤器方法(如果存在)。

var redCars = _.filter(cars, function(car) { return car.colour == "red" });

回答by Dheeraj Nalawade

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);