Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the key of an object array if the value is true

I have an object. I want to get the keys if the value is true.

What I have tried :

const data = [{superadmin: true, admin: false, user: false}]
keys = Object.keys(data).filter(k => data[k]);
console.log(typeof(data));
console.log(data)
console.log(keys)

Expected Output :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

["superadmin"]

>Solution :

This works

const data = [{
  superadmin: true,
  admin: false,
  user: false
}]
keys = data.map(obj => {
  const data = Object.keys(obj).filter((key) => {
    return !!obj[key]
  })

  return data
}).flat(1);

console.log(keys)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading