使用JavaScript和lodash汇总对象数组的数据的最佳方法

时间:2014-10-21 21:35:34

标签: javascript arrays angularjs object lodash

我有一个对象数组,我正在寻找最有效的方法来分组和汇总数据。目前我有几百行代码和很多'每个'涉及的语句,我知道有更简单的方法来做到这一点,但我不能让它工作得很好。

以下是我的数据的一小部分示例。对于任何给定的数据集,我可能有几百个产品。每个产品都有五颗星,星星可以分配三个值中的一个(获得,未获得或正在进行中)。我的目标是查看每个星的每个值的分配总数。

[{
  Product: "A"
  star1: "Not Earned"
  star2: "In Progress"
  star3: "Earned"
  star4: "Not Earned"
  star5: "In Progress"
},{
  Product: "B"
  star1: "In Progress"
  star2: "Not Earned"
  star3: "In Progress"
  star4: "Earned"
  star5: "Earned"
}]

最后我会看到这样的结果:我意识到我的对象和数组的格式有点偏离,这是我寻求帮助的另一部分原因。我也在使用JavaScript和lodash。

Results= [{
   star1:{
      In Progress: 50,
      Not Earned: 32,
      Earned: 1
    },{
   star2:{
      In Progress: 10
      Not Earned: 14,
      Earned: 11
    },{
   star3:{
      In Progress: 45,
      Not Earned: 25,
      Earned: 19
    }]

我该怎么做?

2 个答案:

答案 0 :(得分:1)

这是一种方法,可能不是最快的,但它有效并且不会产生任何临时变量。除了单个数组var名称(r)之外,它不是硬编码到数据,不需要运行任何库(dino浏览器除外),而且它是""简单":

var r=[{
  Product: "A",
  star1: "Not Earned",
  star2: "In Progress",
  star3: "Earned",
  star4: "Not Earned",
  star5: "Earned"
},{
  Product: "B",
  star1: "In Progress",
  star2: "Not Earned",
  star3: "In Progress",
  star4: "Earned",
  star5: "Earned"
},,{
  Product: "C",
  star1: "In Progress",
  star2: "Not Earned",
  star3: "Not Earned",
  star4: "Earned",
  star5: "In Progress"
}];

var sums = {}; // A count holder

Object.keys(r[0]).forEach(function(k){ // For each key in the data of a single data object
     this[k]=r.map(function(o){ return o[k] }) // Pluck values
        .map(function(w){
             if(this[w]){this[w]++;}else{this[w]=1;} // Count values using an object
             return this;
         },{}).pop();  // Take just one of the count object copies (poor-man's reduce with this)
}, sums);

// View result:
JSON.stringify(sums, null, "\t")
/* == {
    "Product": {
        "A": 1,
        "B": 1,
        "C": 1
    },
    "star1": {
        "Not Earned": 1,
        "In Progress": 2
    },
    "star2": {
        "In Progress": 1,
        "Not Earned": 2
    },
    "star3": {
        "Earned": 1,
        "In Progress": 1,
        "Not Earned": 1
    },
    "star4": {
        "Not Earned": 1,
        "Earned": 2
    },
    "star5": {
        "Earned": 2,
        "In Progress": 1
    }
*/

基本思想是获取每个键下的所有值,然后计算值。 这种双程解决方案可能比硬编码的单一用途程序慢一点,但它不需要手动编码的reduce函数。

如果需要,您可以默认为零,我不确定这两个示例对象是否包含您的集合包含的所有可能值...

答案 1 :(得分:0)

您正在寻找“reduce”功能。

像下面这样的东西应该买单。

var products = [{
    Product: "A",
    star1: "Not Earned",
    star2: "In Progress",
    star3: "Earned",
    star4: "Not Earned",
    star5: "In Progress",
},{
    Product: "B",
    star1: "In Progress",
    star2: "Not Earned",
    star3: "In Progress",
    star4: "Earned",
    star5: "Earned",
}];

function countStars(accumulator, product) {
    for(var prop in accumulator) {
        accumulator[prop][product[prop]] += 1;
    };

    return accumulator;
}

var starTemplate = {
    "In Progress": 0,
    "Not Earned": 0,
    "Earned": 0,
};

var initialAccumulator = {
    star1: _.clone(starTemplate),
    star2: _.clone(starTemplate),
    star3: _.clone(starTemplate),
    star4: _.clone(starTemplate),
    star5: _.clone(starTemplate),
};


var result = _.reduce(products, countStars, initialAccumulator);

当然,根据需要,可以通过一些生成器功能来改进。