I have an object that I'm using to keep track of which checkboxes have been checked on a UI.
It looks something like this:
checked: { key1: false, key2: false, key3: false, key4: false, key5: false, key6: false, key7: false, key8: false, key9: false, key10: false },
I need to be able iterate through this object and set certain values to true/false, depending on the behavior the user wants from the UI.
I have two questions:
Is there a more elegant way to instantiate this?
How would I do this using the Object.entries method?
I'd like to do something like this, but it doesn't work:
let obj = {k1: false, k2: false, k3: false}
Object.entries(obj).forEach(([key, value]) => {
obj.value = true;
console.log(`${key} ${value}`);
});
Thank you!
I have an object that I'm using to keep track of which checkboxes have been checked on a UI.
It looks something like this:
checked: { key1: false, key2: false, key3: false, key4: false, key5: false, key6: false, key7: false, key8: false, key9: false, key10: false },
I need to be able iterate through this object and set certain values to true/false, depending on the behavior the user wants from the UI.
I have two questions:
Is there a more elegant way to instantiate this?
How would I do this using the Object.entries method?
I'd like to do something like this, but it doesn't work:
let obj = {k1: false, k2: false, k3: false}
Object.entries(obj).forEach(([key, value]) => {
obj.value = true;
console.log(`${key} ${value}`);
});
Thank you!
Share Improve this question edited Feb 18, 2020 at 17:53 helloWorld asked Feb 18, 2020 at 17:38 helloWorldhelloWorld 1533 silver badges14 bronze badges 4- What do you mean by set values to true/false depending on behavior? And it's probably more extensible to store key/value pairs in an array or a map than in distinct properties. – Jawi Commented Feb 18, 2020 at 17:49
- 1 thanks for your response. I'm working on a "select none" option for the checkboxes on a page. If a user clicks on "select none", I need to set all the items in the object to false. If a user clicks "select all", I need to set them all to true. If a user selects some of them, I need to update the boolean values of the key/value pairs in the object to reflect which are selected and which aren't. I've updated my question with my attempt at using Object.entries the way I need it to work. – helloWorld Commented Feb 18, 2020 at 17:51
- what didn't work on your example? – Tom Mendelson Commented Feb 18, 2020 at 17:55
-
@TomMendelson the result of console.log prints out all the key/value pairs with a
false
value – helloWorld Commented Feb 18, 2020 at 17:56
2 Answers
Reset to default 5If you need to set all the values in the object to true, you can do something like this
Object.keys(obj).forEach(key => obj[key] = true);
Object.keys
will return all the keys in that object in an array. By looping over that array and then using obj[key]
we can access each key of the object and set it to true
To answer your questions -
This is a pretty good way to store multiple checked booleans, as you can access them using object destructuring like
const { key1, key2, ... } = checked
which, IMHO makes the code more readable.Try -
Object.entries(obj).forEach([key, value] => {
// Set obj key/value
obj[key] = true // or use value here
})
Refer -
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries