blankon91
blankon91

Reputation: 515

How to count the array from specific JSON object value?

here is my javascript:

var json = '{"GetReportIdResult":[{"bulan":"4","total":"1728","type":"CHEESE1K","uang":"8796383"},{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},{"bulan":"4","total":"33507","type":"WHP","uang":"235653242"},{"bulan":"5","total":"4761","type":"CHEESE1K","uang":"134877865"},{"bulan":"5","total":"245867","type":"UHT","uang":"1446787280"},{"bulan":"5","total":"47974","type":"WHP","uang":"631929807"},{"bulan":"6","total":"5762","type":"CHEESE1K","uang":"293393832"},{"bulan":"6","total":"236803","type":"UHT","uang":"2219506085"},{"bulan":"6","total":"24853","type":"WHP","uang":"386175022"}]}';
obj = JSON.parse(json);
var arrayobj = obj.GetReportIdResult.length;
alert (arrayobj);

I want to count how many type in the same bulan value, (e.g. there are 3 type = CHEESE1K, UHT, and ESL in bulan = 4)

how to do that?

Upvotes: 0

Views: 2209

Answers (3)

nnnnnn
nnnnnn

Reputation: 150030

There's still a typo in your JSON: you've got two commas in a row between the first two "bulan":"6" objects. But assuming you fix that...

If you're asking how to count distinct types for a particular bulan value you can do something like this:

function countTypesForBulan(resultArray, bulanVal) {
   var i,
       types,
       count = 0;
   for (i=0, types = {}; i < resultArray.length; i++)
      if (resultArray[i].bulan === bulanVal && !types[resultArray[i].type]) {
         types[resultArray[i].type] = true;
         count++;
      }
   return count;
}

console.log( countTypesForBulan(obj.GetReportIdResult, "4") );  // logs 3

The above loops through the array looking for a particular bulan value, and when it finds one it checks if it has already seen the associated type - if not, it adds it to the types object and increments the counter.

Demo: http://jsfiddle.net/pAWrT/

Upvotes: 2

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

This will give you a map object which will contain the count for each bulan value. For example, map['4'].count will return 3.

var i, row, arr = obj.GetReportIdResult, map = {};
for (i = 0; i < arr.length; i++) {
    row = arr[i];
    map[row.bulan] = map[row.bulan] || {count: 0};
    if (map[row.bulan][row.type] === undefined) {
        map[row.bulan][row.type] = row.type;
        map[row.bulan]['count'] += 1;
    }
}
console.log (JSON.stringify(map));​

JSFiddle here.

Upvotes: 1

Ondra Žižka
Ondra Žižka

Reputation: 46796

First of all, put the JSON into a string, else your example code wont work.

var json = '{"GetReportIdResult":[{"bulan":"4","total":"1728","type":"CHEESE1K","uang":"8796383"},{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},{"bulan":"4","total":"33507","type":"WHP","uang":"235653242"},{"bulan":"5","total":"4761","type":"CHEESE1K","uang":"134877865"},{"bulan":"5","total":"245867","type":"UHT","uang":"1446787280"},{"bulan":"5","total":"47974","type":"WHP","uang":"631929807"},{"bulan":"6","total":"5762","type":"CHEESE1K","uang":"293393832"},,{"bulan":"6","total":"236803","type":"UHT","uang":"2219506085"},{"bulan":"6","total":"24853","type":"WHP","uang":"386175022"}]}';

Then, Iterate with for and count in a variable or a hashmap.

Since GetReportIdResult is an array, you can:

for( var i : obj.GetReportIdResult ){
    obj.GetReportIdResult[i] ... // Use at will.

Upvotes: 1

Related Questions