Counting occurance of element in array

PHOTO EMBED

Sun Jun 20 2021 04:20:19 GMT+0000 (Coordinated Universal Time)

Saved by @Primayuda #javascript

let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']

let countedNames = names.reduce(function (allNames, name) {
  if (name in allNames) {
    allNames[name]++
  }
  else {
    allNames[name] = 1
  }
  return allNames
}, {})
// countedNames is:
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
content_copyCOPY

Need to count occurance of words in arrays of words

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce