How can I take a simple object with boolean values and convert it to an array where only those keys whose values are true end up in the array?
E.g.:
myObject = {
option1: true,
option2: false,
option3: true,
option4: true
}
becomes
['option1','option3','option4']
I tried using _.pick(myObject, Boolean)
as suggested here, but that simply produced an empty object. I'm using Typescript, so if there's any Typescript magic I can use to accomplish this, I'm up for that, too.
This is easily achievable with vanilla js.
let myObject = {
option1: true,
option2: false,
option3: true,
option4: true
}
let array = Object.keys(myObject).filter(key => myObject[key]);
console.log(array);
You can see a working example here.
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