user1032531
user1032531

Reputation: 26281

Getting count of object based on condition

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

Upvotes: 2

Views: 6643

Answers (5)

jornare
jornare

Reputation: 2893

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);

Upvotes: 8

Jeetendra Chauhan
Jeetendra Chauhan

Reputation: 1977

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

Upvotes: 0

Kris Krause
Kris Krause

Reputation: 7326

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();

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120168

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++
}

Upvotes: 0

Claudiu
Claudiu

Reputation: 229321

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.

Upvotes: 1

Related Questions