对数组中的值进行分组和计数 [英] Group and count values in an array

查看:169
本文介绍了对数组中的值进行分组和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含对象的数组,如下所示.

I have an array with objects, like the following.

b = {
  "issues": [{
    "fields": {
      "status": {
        "id": "200",
        "name": "Backlog"
      }
    }
  }, {
    "fields": {
      "status": {
        "id": "202",
        "name": "close"
      }
    }
  }, {
    "fields": {
      "status": {
        "id": "201",
        "name": "close"
      }
    }
  }]
};

我想计算状态为close的问题的数量,以及状态为backlog的问题的数量.我想按以下方式将计数保存到一个新数组中.

I want to count how many issues have status close, and how many have backlog. I'd like to save the count in a new array as follows.

a = [
  {Name: 'Backlog', count: 1},
  {Name: 'close', count: 2}
];

我尝试了以下方法.

b.issues.forEach(function(i) {
  var statusName = i.fields.status.name;

  if (statusName in a.Name) {
    a.count = +1;
  } else {
    a.push({
      Name: statusName,
      count: 1
    });
  }
});

但是,这似乎不起作用.我应该如何实现呢?

That however doesn't seem to be working. How should I implement this?

推荐答案

这是使用

This is a perfect opportunity to use Array#reduce. That function will take a function that is applied to all elements of the array in order and can be used to accumulate a value. We can use it to accumulate an object with the various counts in it.

为了简单起见,我们简单地跟踪对象中的计数{name: count, otherName: otherCount}.对于每个元素,我们检查是否已经有name的条目.如果不是,请创建一个计数为0的计数器.否则,增加计数.在reduce之后,我们可以 map 键数组,存储为对象的键,应采用问题中所述的格式.见下文.

To make things easy, we track the counts in an object as simply {name: count, otherName: otherCount}. For every element, we check if we already have an entry for name. If not, create one with count 0. Otherwise, increment the count. After the reduce, we can map the array of keys, stored as keys of the object, to be in the format described in the question. See below.

var b = {
  "issues": [{
    "fields": {
      "status": {
        "id": "200",
        "name": "Backlog"
      }
    }
  }, {
    "fields": {
      "status": {
        "id": "202",
        "name": "close"
      }
    }
  }, {
    "fields": {
      "status": {
        "id": "201",
        "name": "close"
      }
    }
  }]
};

var counts = b.issues.reduce((p, c) => {
  var name = c.fields.status.name;
  if (!p.hasOwnProperty(name)) {
    p[name] = 0;
  }
  p[name]++;
  return p;
}, {});

console.log(counts);

var countsExtended = Object.keys(counts).map(k => {
  return {name: k, count: counts[k]}; });

console.log(countsExtended);

.as-console-wrapper {
  max-height: 100% !important;
}

注释.

  1. Array#reduce不会修改原始数组.
  2. 您可以轻松地修改所传递的函数,以减少更改,例如通过更改

  1. Array#reduce does not modify the original array.
  2. You can easily modify the function passed to reduce to for example not distinguish between Backlog and backlog by changing

var name = c.fields.status.name;

进入

var name = c.fields.status.name.toLowerCase();

例如.更高级的功能也可以轻松实现.

for example. More advanced functionality can also easily be implemented.

这篇关于对数组中的值进行分组和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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