1

I need help.I need to measure json array length based on key value using Angular.js or Javascript.I am explaining my code below.

[{"day_id":"1","b":"sss"},
  {"day_id":"2","b":"ffff"},
  {"day_id":"3","b":"ccc"},
  {"day_id":"3","b":"hhh"},
{"day_id":"4","b":"kkk"}]

Here i need to measure the length where day_id=3 or 2 etc eans how many set of data present where day_id=3 and here for 3 there are 2 set of data present.Please help me.

2
  • Have to try to write a function to loop and get the data ? I think its not possible in js. Commented Mar 3, 2016 at 7:26
  • Look for angular.forEach() Commented Mar 3, 2016 at 7:41

6 Answers 6

1

You can try this:

var testArr = [{"day_id":"1","b":"sss"},
  {"day_id":"2","b":"ffff"},
  {"day_id":"3","b":"ccc"},
  {"day_id":"3","b":"hhh"},
{"day_id":"4","b":"kkk"}];

function getCount(arr,key,value){
  var count = 0;
  arr.forEach(function(obj){
    if(obj[key] == value){
      count++;
    }
  });
  return count;
}

console.log(getCount(testArr,'day_id','3'));
//2

demo

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

Comments

1

This can help you.

function filterData(data, key, val){
   var result = [];
   for(var i=0;i< data.length; i++){
      if ((data[i][key]) && (data[i][key] == val)) {
         result.push(data[i]);
      }
   }
   return result;
}

You can use it as filterData(data, "day_id", 3)

Disclaimer: It can have typos.

Comments

1

You can use the Array.prototype.filter method:

var filtered_array = your_array.filter(function(row) {
    return row.day_id = "3"
});

var day_id_count = filtered_array.length

1 Comment

Or, if the filtered array is not required for other purposes : your_array.filter (function (row) { return row.day_id = "3" }).length
1

This solution is using angular.

// data is your array
function MyCtrl($scope, $filter) {
  $scope.data = [{
    "day_id": "1",
    "b": "sss"
  }, {
    "day_id": "2",
    "b": "ffff"
  }, {
    "day_id": "3",
    "b": "ccc"
  }, {
    "day_id": "3",
    "b": "hhh"
  }, {
    "day_id": "4",
    "b": "kkk"
  }];
  $scope.count = $filter('filter')($scope.data, {
    day_id: 1
  }).length;
}
<!DOCTYPE html>
<html ng-app>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="MyCtrl">
  Count: {{count}}
</body>

</html>

Comments

1

This is a more generic solution, it takes an object with the property to search for and the value.

It iterates over the array and count if the wanted property hast the same value.

function getCount(array, search) {
    var c = 0,
        k = Object.keys(search)[0];

    array.forEach(function (a) {
        a[k] === search[k] && c++;
    });
    return c;
}

var array = [{ "day_id": "1", "b": "sss" }, { "day_id": "2", "b": "ffff" }, { "day_id": "3", "b": "ccc" }, { "day_id": "3", "b": "hhh" }, { "day_id": "4", "b": "kkk" }];
document.write(getCount(array, { day_id: '2' }) + '<br>');
document.write(getCount(array, { day_id: '3' }) + '<br>');

Comments

0

try this.you can use filter function on arrays

var arr = [{
  "day_id": "1",
  "b": "sss"
}, {
  "day_id": "2",
  "b": "ffff"
}, {
  "day_id": "3",
  "b": "ccc"
}, {
  "day_id": "3",
  "b": "hhh"
}, {
  "day_id": "4",
  "b": "kkk"
}]

Array.prototype.findCountByPropertyValue=function(property_name,value)
{
  return arr.filter(function(i) { return i[property_name] == value}).length
}
document.write(arr.findCountByPropertyValue('day_id',1))
document.write('<br>')
document.write(arr.findCountByPropertyValue('day_id',2))
document.write('<br>')
document.write(arr.findCountByPropertyValue('day_id',3))
document.write('<br>')
document.write(arr.findCountByPropertyValue('day_id',4))
document.write('<br>')
document.write(arr.findCountByPropertyValue('day_id',5))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.