-2

I am trying to iterate array of objects with similar key values and want to calculate how many times their values has been repeated in whole array of object e.g. count of ‘Dinner' in whole array of objects

var objects = [
  { Ram: 'Lunch',  Luv:'Lunch',  Rani:'Dinner' },
  { Ram: 'Dinner', Luv:'Lunch',  Rani:'Dinner' },
  { Ram: 'Lunch',  Luv:'Dinner', Rani:'Dinner' }
];

I am trying to calculate how many times Lunch has happened and how many times Dinner has happened and want to display in below json like this -

[{'Dinner':5}, {'Lunch':4}];

How can I achieve this in javascript?

Yoshi
  • 54,081
  • 14
  • 89
  • 103
Aanchal
  • 173
  • 2
  • 14
  • 1
    `objects.map(Object.values).reduce((a, b) => a.concat(b)).reduce((p, c) => (p[c] = (p[c] || 0) + 1, p), {});`, while this kind of question has been done hundreds of times, it's still fun to do. You should search and try yourself though as the guidelines suggest. – ASDFGerte Apr 12 '18 at 09:30
  • May I know the reason for downvoting – Aanchal Apr 12 '18 at 09:46
  • those who couldn't help with answer should not do down vote atleast.. – Aanchal Apr 12 '18 at 09:55
  • See e.g. [this question](https://stackoverflow.com/questions/38694939/count-particular-key-value-from-array-of-object) (2-5min googling, very similar) - [idownvotedbecau.se/noattempt](http://idownvotedbecau.se/noattempt/). I know i am a bit more downvote-happy than most on stackoverflow - this question has higher quality than what often gets upvoted on here otherwise. Generally, don't take rep too serious, it won't benefit you. If you can pay four rep to get a problem done you otherwise would take an hour to solve, that's a pretty good deal. – ASDFGerte Apr 12 '18 at 09:55

3 Answers3

2

You could use reduce() with Object.values() and return one object as a result instead of array of objects.

var objects=[
{Ram:'Lunch', Luv:'Lunch', Rani:'Dinner'},
{Ram:'Dinner', Luv:'Lunch', Rani:'Dinner'},
{Ram:'Lunch', Luv:'Dinner', Rani:'Dinner'}
]

const result = objects.reduce((r, o) => {
  Object.values(o).forEach(e => r[e] = (r[e] || 0) + 1)
  return r;
}, {});

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • If I want to get the result as key and value pair, e.g. [ { “count" : 5, “type": “Dinner", }, { “count" : 4, “type": “Lunch", }] how can I do this , could you help in this logic? – Aanchal Apr 13 '18 at 07:41
  • In that case you could use code like this https://jsfiddle.net/b5rex4q9/3/ – Nenad Vracar Apr 13 '18 at 10:16
1

Because I dislike forEach, here's a double-reduce:

const objects = [
  { Ram: 'Lunch',  Luv: 'Lunch',  Rani: 'Dinner' },
  { Ram: 'Dinner', Luv: 'Lunch',  Rani: 'Dinner' },
  { Ram: 'Lunch',  Luv: 'Dinner', Rani: 'Dinner' }
];

const count = objects
  // collect and flatten object values
  .reduce((a, o) => a.concat(Object.values(o)), [])

  // count individual
  .reduce((c, v) => ({...c, [v]: (c[v] || 0) + 1}), {})
;

const list = Object.entries(count)
  // map to {type, count}
  .map(([type, count]) => ({type, count}))
;

console.log(count, list);
Yoshi
  • 54,081
  • 14
  • 89
  • 103
0

Use reduce with forEach

var output = objects.reduce( (a, c) => 
    (Object.values(c).forEach( 
       s => a[s] = (a[s] || 0) + 1 ), //end forEach
    a), 
{})//end reduce

Demo

var objects = [{
    Ram: "Lunch",
    Luv: "Lunch",
    Rani: "Dinner"
  },
  {
    Ram: "Dinner",
    Luv: "Lunch",
    Rani: "Dinner"
  },
  {
    Ram: "Lunch",
    Luv: "Dinner",
    Rani: "Dinner"
  }
];
var output = objects.reduce((a, c) => (Object.values(c).forEach(s => a[s] = (a[s] || 0) + 1), a), {});
console.log(output);
gurvinder372
  • 66,980
  • 10
  • 72
  • 94