javascript - 计算嵌套 Json 内的唯一值

标签 javascript json counting

{
    "response_code": "100",
    "total_orders": "302",
    "order_ids": "930777,930783,930788,930791,930793",
    "data": {
        "930777": {
            "response_code": "100",
            "acquisition_date": "2017-04-07 00:06:29",
            "ancestor_id": "930777",
            "affiliate": "1038"
        },
        "930783": {
            "response_code": "100",
            "acquisition_date": "2017-04-07 00:07:59",
            "ancestor_id": "930783",
            "affiliate": "1040"
        },
        "930788": {
            "response_code": "100",
            "acquisition_date": "2017-04-07 00:17:04",
            "ancestor_id": "930788",
            "affiliate": "1038"
        },
        "930791": {
            "response_code": "100",
            "acquisition_date": "2017-04-07 00:20:31",
            "ancestor_id": "930791",
            "affiliate": "1030"
        },
        "930793": {
            "response_code": "100",
            "acquisition_date": "2017-04-07 00:24:34",
            "ancestor_id": "930793",
            "affiliate": "1038"
        }
    }
}

嗨,我有一个从上面调用的 API 返回的 JSON。我想知道计算成员(member)的最佳方法是什么。我想要这样的结果

Affiliate | Number
1038      | 3
1040      | 1
1030      | 1

有时返回 API 可能最多有 4000 条记录。我认为必须有一个库可以在短时间内完成这项工作

谢谢

最佳答案

您可以在 Object.keys 上使用 reduce 循环遍历对象并累积结果,如下所示:

var obj = {"response_code":"100","total_orders":"302","order_ids":"930777,930783,930788,930791,930793","data":{"930777":{"response_code":"100","acquisition_date":"2017-04-07 00:06:29","ancestor_id":"930777","affiliate":"1038"},"930783":{"response_code":"100","acquisition_date":"2017-04-07 00:07:59","ancestor_id":"930783","affiliate":"1040"},"930788":{"response_code":"100","acquisition_date":"2017-04-07 00:17:04","ancestor_id":"930788","affiliate":"1038"},"930791":{"response_code":"100","acquisition_date":"2017-04-07 00:20:31","ancestor_id":"930791","affiliate":"1030"},"930793":{"response_code":"100","acquisition_date":"2017-04-07 00:24:34","ancestor_id":"930793","affiliate":"1038"}}}

var result = Object.keys(obj.data).reduce(function(acc, key) {  // for each key in the data object
  var aff = obj.data[key].affiliate;                            // get the affiliate of the according object
  if(acc[aff]) acc[aff]++;                                      // if we already have have a counter for this affiliate (aff) then increment the value from the accumulator acc
  else         acc[aff] = 1;                                    // otherwise start a counter for this affiliate (initialized with 1)
  return acc;
}, {});

console.log(result);

关于javascript - 计算嵌套 Json 内的唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284533/

相关文章:

java - java中的无限while循环和循环错误计数

opencv - 我收到错误。我想反对计数

Pythonic 方法来计算列表中特定邻居的数量

javascript - Google Maps API V3 在本地存储中保存/恢复边界

javascript - 悬停子元素时触发 HTML5 dragleave

javascript - 如何以 Angular 方式从 JSON 访问所有数组值

android - 我如何安全地(间接地)查询 android 中的 postgresql 数据库?

json - 为什么 Groovy JSONBuilder 对 URL 中的斜杠进行转义?

.net - 如何在 mshtml.HTMLDocument (.NET) 中禁用 Javascript

javascript - 过滤对象数组 - 几个条件