Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

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 }
Comment

PREVIOUS NEXT
Code Example
Javascript :: d-block d-none js 
Javascript :: jQuery - Remove 
Javascript :: get decimals from float javascript 
Javascript :: sequelize datetime format 
Javascript :: form- text area react 
Javascript :: react select 
Javascript :: merge two sorted linked lists 
Javascript :: javascript get response payload 
Javascript :: set state 
Javascript :: array from javascript 
Javascript :: esql convert blob to json 
Javascript :: Don’t Use If-Else and Switch in JavaScript, Use Object Literals 
Javascript :: javascript add field to array 
Javascript :: electronjs 
Javascript :: chatbot using html and javascript 
Javascript :: react native when debug crash 
Javascript :: gps nodejs 
Javascript :: typescript compile string to js 
Javascript :: display json data in html table react 
Javascript :: javascript closest parent 
Javascript :: error handling in node.js 
Javascript :: react table handling multiple selected checkbox 
Javascript :: javascript pad string left 
Javascript :: how to change Mime type of a file express 
Javascript :: js get type 
Javascript :: mongoose populate array of ids 
Javascript :: console.log() Syntax 
Javascript :: context api example in react 
Javascript :: class 
Javascript :: get vue-emoji-picker 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =