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?

Andreas
  • 5,393
  • 9
  • 44
  • 53
dausdashsan
  • 241
  • 1
  • 6
  • 16
  • 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? – Teemu Jun 19 '14 at 06:47
  • The most efficient way is to use a for loop. here's the explanation => http://stackoverflow.com/questions/8550183/sum-of-values-in-an-array-using-jquery – HandsomeG Jun 19 '14 at 06:49
  • LINQ.js will work also and from what I saw is pretty fast – HellBaby Jun 19 '14 at 06:50
  • I did do the loop approach. But just wondering if there are another way to tackle this. By the way thanks. – dausdashsan Jun 19 '14 at 06: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. – Andy Gaskell Jun 19 '14 at 06:59
  • 1. if the data is sorted, the counting process will be faster. 2. or you can return one more object (which is already count the data from the server), example: {totalDone:5} – Mr.Cocococo Jun 19 '14 at 07:10

5 Answers5

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);
Mr.Cocococo
  • 1,401
  • 2
  • 11
  • 13
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/

Mourice
  • 597
  • 1
  • 5
  • 17
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);
abhinsit
  • 3,214
  • 4
  • 21
  • 26
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

Diana Nassar
  • 2,303
  • 14
  • 17
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;
    }
chandu
  • 2,276
  • 3
  • 20
  • 35