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

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

问题描述

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

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?

推荐答案

这是使用 Array#reduce.该函数将采用一个函数,该函数按顺序应用于数组的所有元素,并可用于累加一个值.我们可以用它来累积一个包含各种计数的对象.

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天全站免登陆