How do i return an object key name only if the value of it is true?
I'm using underscore and the only thing i see is how to return keys which is easy, i want to avoid redundant iterations as much as possible:
example:
Object {1001: true, 1002: false}
I want an array with only 1001 in it...
To check if all of the values in an object are equal to false , use the Object. values() method to get an array of the object's values and call the every() method on the array, comparing each value to false and returning the result.
Object.keys gets the keys from the object, then you can filter the keys based on the values
var obj = {1001: true, 1002: false}; var keys = Object.keys(obj); var filtered = keys.filter(function(key) { return obj[key] });
FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With