Set all Object keys to false

chefcurry7 picture chefcurry7 · Nov 28, 2016 · Viewed 29.8k times · Source

Lets say I have an object

  filter: {
    "ID": false,
    "Name": true,
    "Role": false,
    "Sector": true,
    "Code": false
  }

I want to set all keys to false (to reset them). What's the best way to do this, I'd like to avoid looping with foreach and stuff. Any neat one liner?

Answer

nnnnnn picture nnnnnn · Nov 28, 2016

Well here's a one-liner with vanilla JS:

Object.keys(filter).forEach(v => filter[v] = false)

It does use an implicit loop with the .forEach() method, but you'd have to loop one way or another (unless you reset by replacing the whole object with a hardcoded default object literal).