0

I'm trying to figure out how to create an object that describes the frequency of certain names in an array. Uppercase or lowercase letters shouldn't matter. Right now each property of the object has the value of 1. I don't know where to go from here.

function getNameFrequency (source) {
  const lowerCaseNames = source.map(name => name.toLowerCase())
  let frequencyObject = {}

  for (const key of lowerCaseNames) {
       frequencyObject[key] = 1
  }

  return frequencyObject
}

console.log(getNameFrequency[Anne, LUKE, luke, simon, Daniel, anne])
 // expected output => { anne: 2, luke: 2, simon: 1, daniel: 1}
  • This is a classic map/reduce problem. Map all objects to eg. anne: 1 and in the reduce step match lowercase keys and if matches then value=value+1. – Nico Van Belle Oct 20 '20 at 11:30
  • You're not incrementing the values. Also the array values shown are variables and not strings. – evolutionxbox Oct 20 '20 at 11:32
  • A better duplicate: https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements – Nick Oct 20 '20 at 11:36

0 Answers0