2

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.

3 Answers 3

5

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>');

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

8 Comments

You've formatted that code in a way that's kinda hard to read.
Thanks, works perfectly, I'll accept this answer as correct. I have a followup question that I hope you don't mind answering. How can I sort the array to have the ones with more iterations in the first places?
@nick Added a simple sorting function.
@nick My pleasure, have a great day (:
@Kinduser, oh I see. I've wondered why did you use null there, but you used it since you needed a third argument to make it more readable. Got ya, thanks. :)
|
0

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);

Comments

-1

you can use forEach

newArr = {}

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

1 Comment

Object keys are not ordered.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.