javascript 计算每个项目在 JSON 中的出现次数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12712056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:57:10  来源:igfitidea点击:

Count occurrences of each item in JSON

javascriptjqueryjson

提问by relyt

I want to return the count of each property. For example, I would like to return how many people are in each group. So for the following JSON, it would return 3 people in Group 1, 3 people in Group 3, 2 people in Group 16, etc.

我想返回每个属性的计数。例如,我想返回每个组中有多少人。因此,对于以下 JSON,它将返回第 1 组中的 3 个人、第 3 组中的 3 个人、第 16 组中的 2 个人,等等。

I'm looking for a Javascript or jQuery solution.

我正在寻找 Javascript 或 jQuery 解决方案。

Note: I do not need help parsing the JSON. I only need help in finding the counts of each occurrence.

注意:我不需要帮助解析 JSON。我只需要帮助查找每次出现的次数。

{
"people":[
    {"id":"0","name":"Mike Johnson","group":1},
    {"id":"1","name":"Bob Smith","group":2},
    {"id":"2","name":"Richard Thomas","group":3},
    {"id":"3","name":"Betty White","group":16},
    {"id":"4","name":"Tim Thompson","group":3},
    {"id":"5","name":"Carl Lewis","group":16},
    {"id":"6","name":"Kathy Towers","group":3},
    {"id":"7","name":"Billy Bob","group":1},
    {"id":"8","name":"Sally Bailey","group":1}
    ]
}

回答by Ian

Like some comment said, you have to loop through the "people" array. It's easy to turn this "feature" into a function like I have so you can just specify the group you're searching for, and get back the number of people.

就像某些评论所说的那样,您必须遍历“people”数组。很容易将这个“功能”变成像我这样的功能,这样你就可以指定你正在搜索的组,然后找回人数。

http://jsfiddle.net/UdhRW/

http://jsfiddle.net/UdhRW/

var obj = {
  "people": [
    {"id":"0","name":"Mike Johnson","group":1},
    {"id":"1","name":"Bob Smith","group":2},
    {"id":"2","name":"Richard Thomas","group":3},
    {"id":"3","name":"Betty White","group":16},
    {"id":"4","name":"Tim Thompson","group":3},
    {"id":"5","name":"Carl Lewis","group":16},
    {"id":"6","name":"Kathy Towers","group":3},
    {"id":"7","name":"Billy Bob","group":1},
    {"id":"8","name":"Sally Bailey","group":1}
    ]
};

function getCount(group) {
    var count = 0;
    for (var i = 0; i < obj.people.length; i++) {
        if (obj.people[i].group == group) {
            count++;
        }
    }
    return count;
}

getCount(1);   // should return 3

If objisn't global, you can pass it to the function and it would be like this:

如果obj不是全局的,你可以将它传递给函数,它会是这样的:

function getCount(arr, group) {
    var count = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].group == group) {
            count++;
        }
    }
    return count;
}

getCount(obj.people, 1);   // should return 3

回答by Blender

You can extract the groups by using something like this:

您可以使用以下方法提取组:

> var groups = foo['people'].map(function(value, index) {return value['group']});
[1, 2, 3, 16, 3, 16, 3, 1, 1]

From there, just create an object with these keys:

从那里,只需使用这些键创建一个对象:

var group_count = {};

groups.forEach(function(value, index) {
    if (value in group_count) {
        group_count[value] += 1;
    } else {
        group_count[value] = 1;
    }
});

This will give you an object that contains the number of occurrences of each group.

这将为您提供一个包含每个组出现次数的对象。

Also, this code won't work IE8 and lower, so you'll have to find a forEachand mappolyfill somewhere on the web.

此外,此代码不适用于 IE8 及更低版本,因此您必须在网络上的某处找到一个forEachmappolyfill。