JSFiddle - React, Tailwind, and code Playground

by hodca

JavaScript

var items = [{
  name: 'name1',
  age: 21
}, {
  name: 'name2',
  age: 21
}, {
  name: 'name3',
  age: 21
}, {
  name: 'name1',
  age: 21
}, {
  name: 'name2',
  age: 21
}]

var dictionary = {};
var result = [];
items.forEach(function(a){
  if(dictionary[a.name])
  {
  	dictionary[a.name].count++;
  }else
  {
  	dictionary[a.name] = {
      "name" : a.name,
      "count" : 1
    }
    result.push(dictionary[a.name]);
  }
 
});

console.log(result);