javascript - Count Unique Value Inside Nested Json -


{     "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"         }     } } 

hi have returned json above api called. wonder best method count affiliate. have result such

affiliate | number 1038      | 3 1040      | 1 1030      | 1 

some time return api might have 4000 records. think there has library can job in short time

thanks

you can loop through object using reduce on object.keys , accumulate results this:

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) {  // each key in data object    var aff = obj.data[key].affiliate;                            // affiliate of according object    if(acc[aff]) acc[aff]++;                                      // if have have counter affiliate (aff) increment value accumulator acc    else         acc[aff] = 1;                                    // otherwise start counter affiliate (initialized 1)    return acc;  }, {});    console.log(result);


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -