javascript - 如何对具有相似键的数据进行分组?

标签 javascript arrays json

我有来自 CSV 的数据:

Group   Profession  Status          Count
6       Fisherman   Offer Accepted  1
6       Fisherman   All             1
7       Fisherman   Offer Accepted  1
7       Fisherman   All             1
8       Banker      Onboard         2
8       Banker      All             2
8       Cook        Onboard         4
8       Cook        All             4
8       Developer   Onboard         2
8       Developer   All             2
9       Banker      Onboard         2
9       Banker      Offer Accepted  1
9       Banker      All             3

我需要将其作为 JSON 数组返回:

"Fisherman" : {
    6 : {
        "Offer Accepted" : 1,
        "All" : 1
    },
    7 : {
        "Offer Accepted" : 1,
        "All" : 1
    }
},
"Banker" : {
    8 : {
        "Onboard" : 2,
        "All" : 2
    },
    9 : {
        "Onboard" : 2,
        "Offer Accepted" : 1,
        "All" : 3
    }
},
....so on

到目前为止,我所做的是获得所有独特的职业和组。

然后我遍历所有数据并比较是否有 Profession AND Group 的匹配项。

for(var d in data) {
    var json = [];
    for(var p in profession) {
        for(var g in group) {
            if(data[d]["Profession"] == profession[p] && data[d]["Group"] == group[g]) {
                json.push({data[d]["Status"] : data[d]["Count"]});
                // put 'json' variable in JSON array with key group? 
            }
        }
    }
}

如果匹配,我会创建一个数组,在其中推送状态和计数。

但我真的不知道如何从那里着手。

感谢您的帮助!

最佳答案

假设数据是一个包含如下对象的数组

{ Group: 6, Profession: 'Fisherman', Status: 'Offer Accepted', Count: 1 }

那么你可以使用下面的

var order = ['Profession', 'Group', 'Status'],
    object = {};

data.forEach(function (d) {
    order.reduce(function (r, a) {
        r[d[a]] = r[d[a]] || {};
        return r[d[a]];
    }, object).Count = d.Count;
});

How it works:

d is an object with the structure like above. oder is an array with keys in the wanted order for the result object. (I renamed json to object, because JSON is a string with a special formatting and not an object, like here necessary.)

For an assignment of count, it is necessary to know the path to the property. This is granted with iterating over the order for the keys of the actual object d.

r[d[a]] = r[d[a]] || {};

d[a] 用于检查属性是否存在,如果不存在则分配一个空对象。

在回调结束时,返回对最后一个对象 r[d[a]] 的引用。

最后,一个新的属性 Count 被赋予 d.Count 的值

object                       a          d[a]           return value
---------------------------- ---------- -------------- ------------
{}                           Profession Fisherman      {}
                /--------------------------------------/            (same reference)
{ "Fisherman": {} }          Group      6              {}
                       /-------------------------------/            (same reference)
{ "Fisherman": { "6": {} } } Status     Offer Accepted {}

object after first loop of data

{
    "Fisherman": {
        "6": {
            "Offer Accepted": {
                "Count": 1
            }
        }
    }
}

综述:reduce返回一些高度可控的东西。

关于javascript - 如何对具有相似键的数据进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325497/

相关文章:

javascript - 为什么浏览器仍然允许 Javascript 查看 cookie?

javascript - 如何验证这个条件呢?

javascript - Highcharts - 需要高级工具提示功能

c - 树排序不排序我的数组

javascript - pm.response.json() 和 JSON.parse(responseBody) 的区别

javascript - 在页面加载时动态插入 <script> 标签到 HTML

javascript - 在 jQuery 中使用数组或对象?

ruby - block 变量中的括号

javascript - 将 JS 数组转换为对象

c# - JSONResult 到字符串