如果属性值为true,则由多个属性组成Lodash组 [英] Lodash group by multiple properties if property value is true

查看:129
本文介绍了如果属性值为true,则由多个属性组成Lodash组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列需要按品牌和型号分组的车辆,只有'selected'属性为真。生成的对象应包含make model和count的属性。使用lodash,如何将车辆对象组织到所需的结果对象中。我能够通过makeCode获取车辆对象,但我不确定如何按多个属性进行分组。

I have an array of vehicles that need to be grouped by make and model, only if the 'selected' property is true. The resulting object should contain properties for make model and count. Using lodash, how can I organize the vehicle objects into the desired result objects. I'm able to get the vehicle objects grouped by makeCode but I'm not sure how to group by more than one property.

group by使代码有效

group by make code works

      var vehicles = _.groupBy(response.vehicleTypes, function(item)
      {
        return item.makeCode; // how to group by model code as well
      });

初始车辆

initial vehicles

{
    id: 1, 
    selected: true, 
    makeCode: "Make-A", 
    modelCode: "Model-a", 
    trimCode: "trim-a", 
    yearCode: "2012"
},
{
    id: 2, 
    selected: false, 
    makeCode: "Make-A", 
    modelCode: "Model-a", 
    trimCode: "trim-a", 
    yearCode: "2013"
},
{
    id: 3, 
    selected: true, 
    makeCode: "Make-B", 
    modelCode: "Model-c", 
    trimCode: "trim-a", 
    yearCode: "2014"
},
{
    id: 25, 
    selected: true, 
    makeCode: "Make-C", 
    modelCode: "Model-b", 
    trimCode: "trim-b", 
    yearCode: "2012"
},
{
    id: 26, 
    selected: true, 
    makeCode: "Make-C", 
    modelCode: "Model-b", 
    trimCode: "trim-a", 
    yearCode: "2013"
}

结果对象

result object

{
    Make-A: {
        Model-a: {
            count: 1
        }
    }
},

{
    Make-B: {
        Model-c: {
            count: 1
        }
    }
},
{
    Make-C: {
        Model-b: {
            count: 2
        }
    }
}


推荐答案

由于你已经在使用lodash,你可以利用 _。过滤功能。这将仅返回选择 的项目为真。

Since you're already using lodash, you can take advantage of the _.filter function. This will return only the items where selected is true.

var selectedVehicles = _.filter(response.vehicleTypes, 'selected');

现在您拥有 selectedVehicles 数组,您可以使用原始代码进行 makeCode 分组。

Now that you have the selectedVehicles array, you can use your original code for grouping by the makeCode.

selectedVehicles = _.groupBy(selectedVehicles, function(item) {
  return item.makeCode;
});

这会返回一个对象,因此我们需要遍历这些键,并执行第二个 groupBy

This returns an object, so we will need to iterate through those keys, and perform our second groupBy

_.forEach(selectedVehicles, function(value, key) {
  selectedVehicles[key] = _.groupBy(selectedVehicles[key], function(item) {
    return item.modelCode;
  });
});

从此您将拥有表格的对象。我会留给你从每个数组中获取计数。

From this you will have an object of the form. I'll leave it to you to get the count from each array.

{ 'Make-A': { 'Model-a': [ ... ] },
  'Make-B': { 'Model-c': [ ... ] },
  'Make-C': { 'Model-b': [ ..., ... ] } }

这篇关于如果属性值为true,则由多个属性组成Lodash组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆