【问题标题】:javascript - Grouping elements in array by propertiesjavascript - 按属性对数组中的元素进行分组
【发布时间】:2017-03-18 18:32:32
【问题描述】:

我正在尝试从下面的 json 中过滤相同的颜色对象,并且每个颜色值都包含两个值(颜色和数值)的组合,但我只想根据颜色进行过滤。

这是我尝试过的

var _ = require('underscore-plus');
var data = [{
"name": "jim",
    "color": "blue 1",
    "age": "22"
}, {
"name": "Sam",
    "color": "blue 2",
    "age": "33"
}, {
"name": "eddie",
    "color": "green 1",
    "age": "77"
},
{
"name": "Dheeraj",
    "color": "blue 3",
    "age": "25"
},
{
"name": "Suraj",
    "color": "green 1",
    "age": "25"
}
];

var result=_.groupBy(data,"color");
console.log(result)

结果应该是具有相同颜色的对象数组。

[{ "name": "jim", "color": "blue 1", "age": "22" },
 { "name": "Sam", "color": "blue 2", "age": "33" },
 { "name": "Dheeraj", "color": "blue 3", "age": "25" }]

[{ "name": "Suraj", "color": "green 1", "age": "25" },
 { "name": "eddie", "color": "green 1", "age": "77" }]

【问题讨论】:

  • 过滤器?分组?请添加想要的结果。
  • 结果应该是具有相同颜色的对象数组。 [{“名称”:“吉姆”,“颜色”:“蓝色 1”,“年龄”:“22”},{“名称”:“山姆”,“颜色”:“蓝色 2”,“年龄”: "33" },{ "name": "Dheeraj", "color": "blue 3", "age": "25" } ] and [{ "name": "Suraj", "color": "green 1 ", "age": "25" },{ "name": "eddie", "color": "green 1", "age": "77" } ]

标签: javascript arrays mapreduce underscore.js javascript-objects


【解决方案1】:

例如你可以使用Jquery.grep()

var result = $.grep(data, function(n){ return n.color == "blue 3" })

【讨论】:

    【解决方案2】:

    只需使用下划线文档here 中详述的groupBy 函数:

    var result = _.groupBy(data, function(datum) { return datum.color; });
    

    您需要提供一个要使用的函数,该函数将返回用于分组元素的属性,在本例中为颜色。

    如果您想过滤,如问题中所述,您可以使用下划线filter 方法:

    var blueOne = _.filter(data, function(datum){ return datum.color == 'blue 1'; });
    

    【讨论】:

      【解决方案3】:

      你可以按颜色分组。

      var data = [{ "name": "jim", "color": "blue 1", "age": "22" }, { "name": "Sam", "color": "blue 2", "age": "33" }, { "name": "eddie", "color": "green 1", "age": "77" }, { "name": "Dheeraj", "color": "blue 3", "age": "25" }, { "name": "Suraj", "color": "green 1", "age": "25" }],
          grouped = {},
          colors;
      
      data.forEach(function (a) {
          var group = a.color.match(/^[a-z]+/i);
          grouped[group] = grouped[group] || [];
          grouped[group].push(a);			
      });
      colors = Object.keys(grouped);
      colors.forEach(function (color) {
          console.log(color, grouped[color]);
      });
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

      • 感谢 Nina 它按预期工作,您能否解释一下代码。
      • 基本上它从color 属性中获取字母并将其用作对象的键。然后它检查grouped 中的属性是否存在,如果不存在,则为其分配一个空数组。稍后将迭代数组的实际元素推送到带有颜色键的grouped 对象。
      【解决方案4】:

      您可以使用Array.prototype.reduce对项目进行分组:

      var data = [{
        "name": "jim",
        "color": "blue 1",
        "age": "22"
      }, {
        "name": "Sam",
        "color": "blue 2",
        "age": "33"
      }, {
        "name": "eddie",
        "color": "green 1",
        "age": "77"
      }, {
        "name": "Dheeraj",
        "color": "blue 3",
        "age": "25"
      }, {
        "name": "Suraj",
        "color": "green 1",
        "age": "25"
      }];
      
      var result = data.reduce(function(grouped, obj) {
        var key = obj.color.split(' ')[0]; // get the color from the key
        grouped[key] = (grouped[key] || []).concat(obj); // use the existing array or create a new array, add the object to it, and assign it to the grouped object
        
        return grouped; // return the grouped object
      }, {});
      
      console.log(result);

      【讨论】:

      • 这似乎是答案的一半。
      • 请详细说明。
      • OP,以一种非常规的方式,为了进一步解释这个问题,在对他自己的问题的评论中给出了预期的结果,我想在你的结果之上需要做更多的事情。
      • @Redu 谢谢。我使用了groupBy 定义。如果 OP 想要一个数组,它更像是一个过滤器。我让他决定。
      • @Redu 对不起,我忘了在评论中添加预期结果,我现在已经添加了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 2016-02-21
      • 1970-01-01
      • 2015-12-31
      • 2017-05-24
      相关资源
      最近更新 更多