8

I have an array of objects like below:

var array =
    [
        {"name":"abc","age":20}
        {"name":"abc","age":20}
        {"name":"abc","age":20}
        {"name":"xyz","age":21}
        {"name":"xyz","age":21}
    ]

I want to count the number of occurrences of distinct values like:

[3,2]

Assuming abc has 3 occurrences and xyz has 2 occurrences.

I am doing it in reactjs. I am able to get distinct values like [abc,xyz] using this answer.

ES6 syntax is preferred.

Community
  • 1
  • 1
Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56

6 Answers6

25

You'll need to know to which name a count belongs, so I propose not to output an array that gives you no clue about that, but an object keyed by names and with as value the corresponding count:

var result = array.reduce( (acc, o) => (acc[o.name] = (acc[o.name] || 0)+1, acc), {} );

var array =
    [
        {"name":"abc","age":20},
        {"name":"abc","age":20},
        {"name":"abc","age":20},
        {"name":"xyz","age":21},
        {"name":"xyz","age":21}
    ];
    
var result = array.reduce( (acc, o) => (acc[o.name] = (acc[o.name] || 0)+1, acc), {} );

console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
7

Map/Reduce to the rescue:

const frequency = array
  .map(({ name }) => name)
  .reduce((names, name) => {
    const count = names[name] || 0;
    names[name] = count + 1;
    return names;
  }, {});

// frequency: { abc: 3, xyz: 2 }
Norguard
  • 26,167
  • 5
  • 41
  • 49
4

You can use forEach/map to iterate the array and store the count in another variable, Check this:

var array = [
     {"name" : "abc", "age" : 20},
     {"name" : "abc", "age" : 20},
     {"name" : "abc", "age" : 20},
     {"name" : "xyz", "age" : 21},
     {"name" : "xyz", "age" : 21},
];
    
let b = {};

array.forEach(el => {
    b[el.name] = (b[el.name] || 0) + 1;
})

console.log(b);
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
0

Still if anyone looking for distinct counts stored in an array

var result = array.reduce( (acc, o) => (acc[o.name] = (acc[o.name] || 0)+1, acc), {} );
result = Object.values(result); // returns an array of values from the object
// result will be [3,2]
mav-raj
  • 751
  • 1
  • 7
  • 20
0

This is one way to do it:

var array =
 [
  {"name":"abc","age":20},
  {"name":"abc","age":20},
  {"name":"abc","age":20},
  {"name":"xyz","age":21},
  {"name":"xyz","age":21}
 ]


let yy = {}
array.map( el => {
 yy[el.name] = (yy[el.name] || 0) + 1
})

console.log(yy)

And this is another way:

var array =
 [
  {"name":"abc","age":20},
  {"name":"abc","age":20},
  {"name":"abc","age":20},
  {"name":"xyz","age":21},
  {"name":"xyz","age":21}
 ]


let yy = {}
array.map( el => {
 if (yy[el.name] === undefined || yy[el.name] === 0) {
  yy[el.name] = 1
 } else {
  yy[el.name] = yy[el.name] + 1
 }
})

console.log(yy)
isethi
  • 661
  • 1
  • 10
  • 22
-1

Quick answer : new Set(array).size.

Explanation (From MDN Web Docs):

The Set object lets you store unique values of any type, whether primitive values or object references

ghilesZ
  • 1,502
  • 1
  • 18
  • 30
  • That will just return the length of the array. That is not what the asker wants. – trincot Jan 24 '19 at 18:32
  • @trincot Nope. It will retrun the cardianality of the Set, which contains unique elements. – ghilesZ Jan 25 '19 at 10:12
  • 1
    I think you misinterpreted the question: the size of the Set will be the same as the length of the Array as all objects in the OP's array are distinct. You would only get a difference if some objects were referenced multiple times in that array, but that is not so for what the OP presents: their question is not to look for unique object references, but for objects that have a unique property value (i.e. `name`) in those *distinct* objects. – trincot Jan 25 '19 at 10:37