38

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?

2
  • thanks, please write an answer!
    – chefcurry7
    Commented Nov 28, 2016 at 2:18
  • How about this, filter: {} if you had handled the filter for the same?
    – Aruna
    Commented Nov 28, 2016 at 2:22

9 Answers 9

72

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

5
  • 3
    Correct me if I'm wrong but this can't be done WITHOUT looping. Not sure if the OP understands this. Commented Nov 28, 2016 at 2:20
  • @CarlEdwards that's fine carl. I didn't realise arrow functions were this useful.
    – chefcurry7
    Commented Nov 28, 2016 at 2:22
  • Even without an arrow function it is still pretty short, as in the other answer.
    – nnnnnn
    Commented Nov 28, 2016 at 2:23
  • Yeah in this case the arrow syntax is almost identical to the traditional function() {}. Just a little less typing. Commented Nov 28, 2016 at 2:24
  • typescript will complain that Element implicitly has an 'any' type because expression of type 'number' can't be used to index type 'Object' Commented Jul 6 at 10:12
36

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 }
4
  • thanks for that. one question, if i want to obtain the property that has value "true" (only one at any time), how best to do it?
    – chefcurry7
    Commented Nov 28, 2016 at 2:44
  • 1
    The example data belie the assertion that only one property is true at a time. But regardless of however many are, _.keys(filter).filter(k => filter[k]) will give you a quick array of all keys to values that are truthy. You can use exact equivalence to true if you want strictly booleans. And you can always index the resulting array to get the first one: _.keys(filter).filter(k => filter[k])[0]. That returns undefined if there are none. Sorry for the three instances of "filter" in that expression, but your variable name overloads the function name. Commented Nov 28, 2016 at 3:05
  • @chefcurry7 You might also like _.find(_.keys(filter), k => filter[k]). It works similarly, but slighly shorter. Commented Nov 28, 2016 at 3:46
  • This is the best solution if you use lodash! Commented Oct 15, 2018 at 15:26
13

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

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

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 }
1
  • 1
    this is the best answer so far! Commented Oct 3, 2022 at 9:06
5

A small line of code compatible with all browsers:

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

With ES6 features one-liner without mutation:

{
  ...Object.keys(filter).reduce((reduced, key) => ({ ...reduced, [key]: false }), {})
}
2
  • ` ...Object.keys(filter).reduce((reduced, key) => ({ ...reduced, [key]: false }), {})` should suffce. No need to wrap it in an object Commented Aug 22, 2019 at 4:44
  • This worked for my use-case, requiring immutability, though the OP did not state that as a requirement.
    – Jozzhart
    Commented Feb 11, 2020 at 5:54
2

hasOwnProperty must be used

```

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

```

1

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;
              }, {});
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.