0

I have a json array contains many elements. A part of the array is given:

var some_array = {
                  "0":{
            "picture":"qwerty.jpg",
            "textofPicture":"comment for Picture 5",
            "picNo":1,
            "id":25,
            "uid0":125,
            "uid1":123,
            "uid2":126,
            "uid3":127,
            "uid4":124,
            "u0":"149",
            "u1":"80",
            "u2":"71",
            "u3":"108",
            "u4":"158",
            "accepted":false,
            "su":"",
            "point":0
          },
          "1":{
            "picture":"qwerty.jpg",
            "textofPicture":"comment for Picture 3",
            "picNo":2,
            "id":23,
            "uid0":113,
            "uid1":117,
            "uid2":116,
            "uid3":114,
            "uid4":115,
            "u0":"62",
            "u1":"58",
            "u2":"115",
            "u3":"138",
            "u4":"106",
            "accepted":false,
            "su":"",
            "point":0
          }
}

I want to count how many accepted key's value is true. I am sure there is good way to do this. I do not want to dive into loops.

zkanoca
  • 9,664
  • 9
  • 50
  • 94
  • Make it an actual array of objects (collection), then you can use `Array.prototype.filter` – elclanrs Nov 17 '13 at 03:13
  • [There is another thread](http://stackoverflow.com/questions/10679580/javascript-search-inside-a-json-object) that explains how to do it with vanilla JS or jQuery. – captbrogers Nov 17 '13 at 03:14

1 Answers1

1

One way to obtain the count you're looking for is like this

var count = 0;
var some_array = [
    0 : {
       accepted : false
    },
    1 : {
       accepted : true
    }
];
for (var i in some_array) {
  if (some_array[i].accepted === true) {
    count++;
  }
}
return count;

Let me know if this helps and makes since to you. if need be i can make plunker for a visual.

Doug Kiser
  • 182
  • 1
  • 1
  • 9
  • Oh sorry I posted to soon you said you did not want to dive into loops and I obviously posted a loop. My mistake. – Doug Kiser Nov 17 '13 at 03:59
  • To the comment below from Slider, did you not just repost what i just posted? – Doug Kiser Nov 17 '13 at 05:44
  • slider posted all of eight seconds after you did. He didn't rip you off. – Michael Petrotta Nov 17 '13 at 05:57
  • @DougKiser I apologize for posting the same code, although that wasn't my intention. We posted at almost the same time. However, since you did beat me to it (by 8 seconds) you reserve the right to have your version over mine. So here, I have deleted my answer. Enjoy the upvotes :) – slider Nov 17 '13 at 07:42
  • @slider I apologize that it came off like I was upset or something that was not my intention, sorry man. There was no need to delete you post. I didn't even post what he wanted. He said he did not want to use loops and I gave him a loop. Thats why after my post I posted "Oh sorry I posted to soon you said you did not want to dive into loops and I obviously posted a loop. My mistake." I'm on here to learn and grow not criticize. Again my apologies. – Doug Kiser Nov 17 '13 at 15:12
  • @MichaelPetrotta I was just asking a question, I appologize that it came off another way. I never said, "You ripped me off". I'm not here for votes, who cares. I am here to learn and help others learn as well. Next time I will rephrase my questions better. Thanks for the input. – Doug Kiser Nov 17 '13 at 15:56
  • @DougKiser Well it does not cost too much to use your code. I have gone with your suggestion. Thanks. – zkanoca Nov 18 '13 at 07:04