nick
nick

Reputation: 3259

Count repetitions of objects in Javascript array

I have this array:

var arr = [
  {class: 'class-1', team: 'abcdef'},
  {class: 'class-21', team: 'xmsqmd'},
  {class: 'class-4', team: 'xaksjs'},
  {class: 'class-21', team: 'xmsqmd'},
  {class: 'class-5', team: 'asioda'},
  {class: 'class-44', team: 'csdcsw'}
];

as you can see, this object:

{class: 'class-21', team: 'xmsqmd'}

is repeated 2 times. Anyways this is just an example, but I need to create another array that should look like this:

newArr = [
  [{class: 'class-1', team: 'abcdef'}, 1],
  [{class: 'class-21', team: 'xmsqmd'}, 2],
  [{class: 'class-4', team: 'xaksjs'}, 1],
  [{class: 'class-5', team: 'asioda'}, 1],
  [{class: 'class-44', team: 'csdcsw'}, 1]
]

so the new array should be a 2d array with the first value being the object and the second being the times it's repeated.

thanks in advance, I've searched but I only found a solution with the result being an array with an object with the key being the stringified object.

Upvotes: 2

Views: 178

Answers (3)

Pankaj Shukla
Pankaj Shukla

Reputation: 2672

We can simplify the solution in just two loops:

var arr = [
  {class: 'class-1', team: 'abcdef'},
  {class: 'class-21', team: 'xmsqmd'},
  {class: 'class-4', team: 'xaksjs'},
  {class: 'class-21', team: 'xmsqmd'},
  {class: 'class-5', team: 'asioda'},
  {class: 'class-44', team: 'csdcsw'}
];

var res = [];
var obj = {};
//construct the object with key and recurrence count as value
arr.forEach(function(value, i) {
    var str = JSON.stringify(value);
	if(!obj[str]) {
		obj[str] = 1;
    } else {
		obj[str]++;
    }
});
//Format the data in the desired output
for(var i in obj) {
	var tempArr = [JSON.parse(i)];
	tempArr.push(obj[i]);
	res.push(tempArr);
}

console.log(res);

Upvotes: 0

kind user
kind user

Reputation: 41913

Possible solution.

  • Use Array#forEach to create a hash object with each element as the key and it's number of appearances in the arr array as it's value.
  • Map the hash object with Array#map, to get the specified object from the original array and it's number of occurences from the hash object.
  • Sort the objects in a descending order by the number of appearances.

var arr = [{
    class: 'class-1',
    team: 'abcdef'
  }, {
    class: 'class-21',
    team: 'xmsqmd'
  }, {
    class: 'class-4',
    team: 'xaksjs'
  }, {
    class: 'class-21',
    team: 'xmsqmd'
  }, {
    class: 'class-5',
    team: 'asioda'
  }, {
    class: 'class-44',
    team: 'csdcsw'
  }],
  hash = arr.map(v => v.class),
  obj = {};
  
  hash.forEach(v => !obj[v] ? obj[v] = 1 : obj[v]++);
  var res = Object.keys(obj).map(v => [Object.assign({}, arr.find(c => c.class == v)), obj[v]])
                            .sort((a,b) => b[1] - a[1]);

  document.write('<pre>' + JSON.stringify(res, null, 2) + '</pre>');

Upvotes: 5

samnu pel
samnu pel

Reputation: 914

you can use forEach

newArr = {}

arr.forEach(function(obj) {
    var key = JSON.stringify(obj)
    newArr[key] = (newArr[key] || 0) + 1
})

Upvotes: -1

Related Questions