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.

2
  • Make it an actual array of objects (collection), then you can use Array.prototype.filter Commented Nov 17, 2013 at 3:13
  • There is another thread that explains how to do it with vanilla JS or jQuery. Commented Nov 17, 2013 at 3:14

1 Answer 1

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.

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

7 Comments

Oh sorry I posted to soon you said you did not want to dive into loops and I obviously posted a loop. My mistake.
To the comment below from Slider, did you not just repost what i just posted?
slider posted all of eight seconds after you did. He didn't rip you off.
@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 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.
|

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.