Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
How to get keys of specific fields in an object?

I need to get an array with specific key values of an object.

Assume there is this object (optional some more different keys)

{
    username: 'bla',
    admin: true,
    editor: true,
    user: false,
    foo: 'bar'
}

I only need to process the keys admin, editor and user and get those keys in an array, if their value is true. So in the example the result should be:

['admin', 'editor']

If all three keys have a false value, it should return an empty array.

I would do a filter first:

obj.filter(e => ['admin', 'editor', 'user'].indexOf(e) > -1 && !!e)

and then extract the keys?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

As you already have the list of fields, just apply a .filter call on that:

const obj = {
    username: 'bla',
    admin: true,
    editor: true,
    user: false,
    foo: 'bar'
};

const fields = ['admin', 'editor', 'user'];
const result = fields.filter(field => obj[field] === true);
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You can get keys from object using Object.keys and apply filter for your require data. Try below code it should work for you.

const obj = 
  {
    username: 'bla',
    admin: true,
    editor: true,
    user: false,
    foo: 'bar'
}
const objKeys = Object.keys(obj).filter(item => (obj[item] && typeof obj[item] == "boolean"));

get only keys:-

console.log(objKeys)

get array of object with key value:-

console.log(objKeys.map(item => ({[item] : obj[item]})))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!