1

This is my JSON:

[{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},
 {"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},
 {"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}]

What would be the most efficient way to get the count of IsDone == true?

6
  • You code doesn't look like JSON object, rather it's an array of objects. Also, this is very basic task in JavaScript, what have you tried? Commented Jun 19, 2014 at 6:47
  • The most efficient way is to use a for loop. here's the explanation => stackoverflow.com/questions/8550183/… Commented Jun 19, 2014 at 6:49
  • LINQ.js will work also and from what I saw is pretty fast Commented Jun 19, 2014 at 6:50
  • I did do the loop approach. But just wondering if there are another way to tackle this. By the way thanks. Commented Jun 19, 2014 at 6:52
  • 1
    "most efficient" in what way? I doubt any of the posted answers will be faster than a regular for loop. Unless the data is sorted you will need to loop through all elements in the array. Commented Jun 19, 2014 at 6:59

5 Answers 5

3

You can use plain javascript iteration:

var a=[{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},{"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},{"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}]

var ct=0;
a.forEach(function(entry) {
    if(entry.IsDone)ct++;
});
alert(ct);
Sign up to request clarification or add additional context in comments.

Comments

1

Use grep fron jquery

var selectedArray = [{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},
{"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},
{"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}]



selectedArray = jQuery.grep(selectedArray, function (el) { return el.IsDone == true;   });

 alert(selectedArray.length);

where seletedArray is your actual array

http://jsfiddle.net/QxV6K/

Comments

0
var data = [{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},{"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},{"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}];

var count = data.filter(function (el) {
  return (el.IsDone === true);
});

alert(count.length);

DEMO

Comments

0

try this one

 var obj = [{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},
    {"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},
    {"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}]

    var count = 0;
    for(i=0;i<obj.length;i--){
      count += (obj[i].IsDone) ? 1 : 0;
    }

Comments

0

In case you're trying to access an item from the example structure by id or name, without knowing it's position in the array, the easiest way to do it would be to use underscore.js library:

    var data =   [{"Id":1,"Order":1,"IsDone":true,"Text":"abc","Date":"6/14/2014"},{"Id":2,"Order":2,"IsDone":false,"Text":"cde","Date":"6/15/2014"},{"Id":3,"Order":3,"IsDone":false,"Text":"fgh","Date":"6/16/2014"}];

    var allDesiredElements = _.filter(data, function(item) {
      return item.IsDone === true;
    });

//for length use
console.log(allDesiredElements.length);

2 Comments

_.find just finds the first one, not all of them.
I think you actually want _.filter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.