0

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?

4
  • Your json variable doesn't actually hold JSON (which would be a string), it is assigned a reference to an object, so you don't need to use JSON.parse(json). Commented Jul 4, 2012 at 4:32
  • @nnnnnn ops, my fault..haha I forget about that :p thank you to reminds me Commented Jul 4, 2012 at 4:42
  • There is a blank array element in this data! Commented Jul 4, 2012 at 4:44
  • @SomethVictory sorry, typo :p thanks to notice me Commented Jul 4, 2012 at 4:46

3 Answers 3

2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

I try it and it shows error log that says Uncaught ReferenceError: type is not defined
Sorry, try my updated version. I had accidentally put type where I meant resultArray[i].type. Fixed, as you can see in the demo.
1

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.

2 Comments

Looks like you have some Java syntax in there :)
thank you for your answer :) but I still not understand how to use it to get what i want
1

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.

1 Comment

well, the result is not like what I want, but this code is very useful for me, because after this I need to map the object :D thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.