2

I have an object containing a bunch of similar objects. I would like to get the count of the object only for those where a object property (status) is of a given value (true). For instance, the count of the below object is 3.

{
 6:{"name":"Mary", "status":true},
 2:{"name":"Mike", "status":true},
 1:{"name":"John", "status":false},
 4:{"name":"Mark", "status":true},
 5:{"name":"Jane", "status":false}
}

Thanks

3
  • 2
    You can upvote halpful questions and answers (by clicking on the arrows) and you can accept answers that solve your problem by clicking on the gree checkmark next to it. This helps organize Stackoverflow and italso makes everyone happy. Commented Jan 12, 2012 at 17:22
  • By the way, what have you tried here? Commented Jan 12, 2012 at 17:22
  • I did the following, and it works, but I expect there are bettersolutions. Sorry for the mixed up post. var count=0; for (var o in myObject) {count=count+myObject[o].status;} Commented Jan 12, 2012 at 17:45

5 Answers 5

8

I recognize you are iterating over an object, not an array, but since the others provide solutions for arrays I recon a solution with array.reduce is in place. Works in most modern browsers (IE9+)

var myArray = [
 {"name":"Mary", "status":true},
 {"name":"Mike", "status":true},
 {"name":"John", "status":false},
 {"name":"Mark", "status":true},
 {"name":"Jane", "status":false}
];

var result = myArray.reduce(function(previousValue, currentObject){
    return previousValue + (currentObject.status ? 1: 0); 
}, 0);
Sign up to request clarification or add additional context in comments.

Comments

1

Specifically:

var i = 0;
var count = 0;
while (i < array.length) {
    if (array[i]['status'] == true) count += 1; 
    i += 1;
}

More generally, you can use some functional programming:

function count_matches(array, func) {
    var i = 0;
    var count = 0;
    while (i < array.length) {
        if (func(array[i])) count += 1;
        i += 1;
    }
    return count;
}

function status_true(obj) {
    return obj['status'] == true;
}

count_matches(array, status_true);

The above snippets do the same thing, but the latter is more flexible/potentially neater.

1 Comment

You could also do i++, to save a couple characters. :D
0

just loop over the array and count how many times the status property is true.

var count = 0;
for (var i = 0; i < yourArray.length; i++){
   var current = yourArray[i];
   if (current.status) count++
}

Comments

0

LinqJs would work (might be too much for the simple example posted in the question) -

http://linqjs.codeplex.com/

var jsonArray = [
{ "user": { "id": 100, "screen_name": "d_linq" }, "text": "to objects" },
{ "user": { "id": 130, "screen_name": "c_bill" }, "text": "g" },
{ "user": { "id": 155, "screen_name": "b_mskk" }, "text": "kabushiki kaisha" },
{ "user": { "id": 301, "screen_name": "a_xbox" }, "text": "halo reach" }]


// ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
var queryResult = Enumerable.From(jsonArray)
.Where(function (x) { return x.user.id < 200 })
.OrderBy(function (x) { return x.user.screen_name })
.Select(function (x) { return x.user.screen_name + ':' + x.text })
.ToArray();

// shortcut! string lambda selector
var queryResult2 = Enumerable.From(jsonArray)
.Where("$.user.id < 200")
.OrderBy("$.user.screen_name")
.Select("$.user.screen_name + ':' + $.text")
.ToArray();

Comments

0
var obj = {
 6:{"name":"Mary", "status":true},
 2:{"name":"Mike", "status":true},
 1:{"name":"John", "status":false},
 4:{"name":"Mark", "status":true},
 5:{"name":"Jane", "status":false}
};
var count = 0;
for (var prop in obj) {
  if(obj[prop].status === true){
   count += 1; 
  }
}
console.log("Output: "+count);
$("#debug").text("Output: "+count);

live demo http://jsbin.com/uwucid/2/edit

Comments

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.