frostrock
frostrock

Reputation: 879

How count objects with element value?

I need your help. I have for example such array:

  var array = [{
    "name": "Tony",
    "year": "2010"
}, {
    "name": "Helen",
    "year": "2010"
}, {
    "name": "Jack",
    "year": "2005"
}, {
    "name": "Tony",
    "year": "2008"
}, {
    "name": "Max",
    "year": "2005"
}];

How i can count them by year and get something like this:

2010 = 2 times;
2005 = 2 times;
2008 = 1 time;

Thank you

Upvotes: 2

Views: 69

Answers (5)

Andy
Andy

Reputation: 63570

Here arrange uses reduce to build an object using the years as keys. It accepts a prop argument so that you can build the object as you see fit.

function arrange(arr, prop) {
  return arr.reduce(function(p, c) {
    var key = c[prop];
    p[key] = p[key] || 0;
    p[key]++;
    return p;
  }, {});
}

You can then iterate over the key/values of that object and print out the results for year:

var obj = arrange(array, 'year');
for (var p in obj) {
  console.log(p + ' = ' + obj[p] + ' times');
}

Or even by name:

var obj = arrange(array, 'name');

DEMO

Upvotes: 1

var array = [{
    "name": "Tony",
    "year": "2010"
}, {
    "name": "Helen",
    "year": "2010"
}, {
    "name": "Jack",
    "year": "2005"
}, {
    "name": "Tony",
    "year": "2008"
}, {
    "name": "Max",
    "year": "2005"
}];

var result = {};
array.forEach(function(data) {
    console.log(data.year);
    result[data.year] = result[data.year] ? ++result[data.year] : 1;
});

document.write(JSON.stringify(result));

Upvotes: 0

Redd Sanso
Redd Sanso

Reputation: 51

You are trying to re-invent the wheel. This and many more methods are exposed by a third-party JS library, called "lodash"; old name was "underscore". All its methods or most of them are exposed like

_.methodName(listName, iteratorFunction)

You can download it at: https://lodash.com/

Once you download and include lodash your script in your html, go to your function and do:

_.groupBy(yourArrayVariable, function(item){
    return item.year;
});

WARNING This method will not return an array. It will return a JSON, in which the keys are represented by the "item.year" through the whole original array, and the values will be an array for each year listed. Every such array is a list of objects having the same "year" value:

{
   "2010" : [
        { "name" : "Tony", "year" : "2010" },
        { "name" : "Helen", "year" : "2010" }
   ],
   "2011" : [ ..... ]
}

Lodash has lots of useful methods anyway.

Upvotes: 0

Sapikelio
Sapikelio

Reputation: 2604

Try this:

var array = [{
    "name": "Tony",
    "year": "2010"
}, {
    "name": "Helen",
    "year": "2010"
}, {
    "name": "Jack",
    "year": "2005"
}, {
    "name": "Tony",
    "year": "2008"
}, {
    "name": "Max",
    "year": "2005"
}];
var map={}
for (var i = 0; i<array.length;i++){
   if ( !map[ array [ i ].year ] ){
      map[ array [ i ].year ] = 0;
   }
   map[ array [ i ].year] ++;
}
console.log( map );

Fiddle

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68423

check this fiddle

var countObj = {};
for( var counter = 0; counter < array.length; counter++ )
{
   var yearValue = array [ counter ].year;
   if ( !countObj[ yearValue ] )
   {
      countObj[ yearValue ] = 0;
   }
   countObj[ yearValue ] ++;
}
console.log( countObj );

Upvotes: 1

Related Questions