Answers for "how do you count the number of values in an object?"

7

javascript get number of elements in object

Object.keys(obj).length
Posted by: Guest on September-15-2020
0

Counting instances of values in an object

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 }
Posted by: Guest on May-21-2021

Code answers related to "how do you count the number of values in an object?"

Code answers related to "Javascript"

Browse Popular Code Answers by Language