0

I want to convert an object like this:

{ status: true, count: true, period: false, price: false }


[ 'status', 'count' ]

How can I find the true key value in the object and convert to an Object to an array?

  • [Filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) [Object.entries](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries). – Teemu Mar 25 '22 at 05:20
  • `Object.entries(array).filter(([,$])=>$).map(([$])=>$)` – Bravo Mar 25 '22 at 05:21
  • `Object.entries(theObject).filter(([ _key, value ]) => value === true).map(([ key ]) => key)`. Get familiar with [how to access and process objects, arrays, or JSON](/q/11922383/4642212) and use the static and instance methods of [`Object`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Mar 25 '22 at 05:21
  • @SebastianSimon - you can drop the `_key` :p – Bravo Mar 25 '22 at 05:23
  • @Bravo That’s a matter of preference. – Sebastian Simon Mar 25 '22 at 05:24
  • my linter is pretty picky about *unused variables* (though, I know it lets this through :p ) – Bravo Mar 25 '22 at 05:27
  • const obj = { status: true, count: true, period: false, price: false } const result = Object.keys(obj).filter((key) => { if (obj[key] === true) { return key; } }); console.log(result); – Mamunur Rashid Mar 25 '22 at 05:52

0 Answers0