2017-04-07 109 views
1
{ 
    "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。我想知道什么是最好的方法来计算子公司。我想得到的结果如下:在嵌套JSON中计算唯一值

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

有时候返回API可能有多达4000条记录。我认为必须有能够在很短的时间完成这项工作

感谢图书馆

回答

0

你可以通过使用上Object.keysreduce和积累的结果,这样的对象循环:

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);

+0

感谢你它的工作原理 –

0

您可以遍历所有键并构造带有子会员编号作为键和出现次数作为值的对象。

// jshint esnext: true 
 
const example = {'response_code':'100','total_orders':'302','order_ids':'930777,930783,930788,930791,930793','data':{'930777':{'response_code':'100','acquisition_date':'2017-04-0700:06:29','ancestor_id':'930777','affiliate':'1038'},'930783':{'response_code':'100','acquisition_date':'2017-04-0700:07:59','ancestor_id':'930783','affiliate':'1040'},'930788':{'response_code':'100','acquisition_date':'2017-04-0700:17:04','ancestor_id':'930788','affiliate':'1038'},'930791':{'response_code':'100','acquisition_date':'2017-04-0700:20:31','ancestor_id':'930791','affiliate':'1030'},'930793':{'response_code':'100','acquisition_date':'2017-04-0700:24:34','ancestor_id':'930793','affiliate':'1038'}}}; 
 
const results = {}; 
 

 
Object.keys(example.data).forEach(key => { 
 
    const resultsEntry = results[example.data[key].affiliate]; 
 
    if (resultsEntry === undefined) { 
 
    results[example.data[key].affiliate] = 1; 
 
    } 
 
    else { 
 
    results[example.data[key].affiliate]++; 
 
    } 
 
}); 
 

 
console.log(results);
/* Console output formatting */ 
 
.as-console-wrapper { top: 0; }

+1

感谢你它的工作原理 –

0

下面是一个例子使用下划线:

var data = // Your json; 

var groupedData = _.groupBy(data.data, function(d){return d.affiliate}); 

的jsfiddle:http://jsfiddle.net/afj4dy6v/

+0

谢谢你它的工作原理 –