chefcurry7
chefcurry7

Reputation: 5241

Set all Object keys to false

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?

Upvotes: 39

Views: 64258

Answers (9)

Exil
Exil

Reputation: 329

Here's what i would call a neat one-liner solution:

const filtered = Object.assign(...Object.keys(filter).map(k => ({ [k]: false })));

Demo:

const filter = {
  'ID': false,
  'Name': true,
  'Role': false,
  'Sector': true,
  'Code': false
};

const filtered = Object.assign(...Object.keys(filter).map(k => ({ [k]: false })));

console.log(filtered);

We are basically converting the object into an array using Object.assign() so we can use the map() fucntion on it to set each propety value to false, then we convert the array back to an object using the Object.assign() with the spread operator :)

Upvotes: 0

Giovanni Benussi
Giovanni Benussi

Reputation: 3500

If you don't want to modify the array, here's an ES6 alternative that returns a new one:

Object.fromEntries(Object.keys(filter).map((key) => [key, false]))

Explanation:

Object.keys returns the object's keys:

Object.keys({ a: 1, b: 2 }) // returns ["a", "b"]

Then we map the result ["a", "b"] to [key, false]:

["a", "b"].map((key) => [key, false]) // returns [['a', false], ['b', false]]

And finally we call Object.fromEntries that maps an array of arrays with the form [key, value] to an Object:

Object.fromEntries([['a', false], ['b', false]]) // returns { a: false, b: false }

Upvotes: 9

Joshua Russell
Joshua Russell

Reputation: 700

If you're not using ES6, here is its ES5 counterpart.

Object.keys(filter).forEach(function(key, value) {
    return filter[key] = false;
})

Upvotes: 13

RegarBoy
RegarBoy

Reputation: 3521

With ES6 features one-liner without mutation:

{
  ...Object.keys(filter).reduce((reduced, key) => ({ ...reduced, [key]: false }), {})
}

Upvotes: 4

Avinash Srivastava
Avinash Srivastava

Reputation: 31

In case you are dealing with 'scope' variables, this might be a better solution.

Object.keys(filter).reduce(function(accObj, parseObj) {
                accObj[parseObj] = false;
                return accObj;
              }, {});

Upvotes: 1

Alex Shwarc
Alex Shwarc

Reputation: 885

hasOwnProperty must be used

```

for(var i in your_object) {
  if (Object.hasOwnProperty.call(your_object, i)) {
    your_object[i] = false;
  }
}

```

Upvotes: 2

Jonathan Eunice
Jonathan Eunice

Reputation: 22463

Using lodash, mapValues is a graceful, loop-free way:

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

filter = _.mapValues(filter, () => false);

If you want to do this with Underscore.js, there is an equivalent, but with a slightly different name:

filter = _.mapObject(filter, () => false);

In either case, the value of filter will be set to:

{ ID: false, 
  Name: false, 
  Role: false, 
  Sector: false, 
  Code: false }

Upvotes: 36

Pinguto
Pinguto

Reputation: 436

A small line of code compatible with all browsers:

for(var i in your_object) your_object[i] = false;

Upvotes: 5

nnnnnn
nnnnnn

Reputation: 150040

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).

Upvotes: 72

Related Questions