user2954587 user2954587 -4 years ago 105
Javascript Question

Map object keys to array is value is true

I have object pks and would like to extract all keys where the value is true

pks = {3: false, 7: true, 2: true}


Is there an underscore function that can return
[7, 2]
? I've tried
_.invert
but I lost one of the values in the process so I'm looking for an alternative

Answer Source

You can do this with builtin functions, no need for Underscore:

Object.keys(pks)
  .filter(function(k){return pks[k]})
  .map(Number)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download