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

132
Views
Need to check if any one of two keys are there in object javascript

I am trying to check if any one of the two given keys are available in a given object

let keys = ['foo', 'bar'];

given object

let obj = {
  "foo": "value"
}

let obj1 = {
  "key": "value"
}

As we can see in obj foo is present and in obj1 none of the given keys are present.

"key" in obj //can check for single key like this

Need to check anyone of given key should be present in object

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

0

You can use Object.keys combined with Array.prototype.includes:

let obj = {
  foo: 'value',
  key: 'value'
}

let keys = ['foo', 'bar']

// true
console.log(Object.keys(obj).some(k => keys.includes(k)));

keys = ['bar','bar'];
// false
console.log(Object.keys(obj).some(k => keys.includes(k)));

about 4 years ago · Juan Pablo Isaza Report

0

Use Array#some to iterate over the keys list and Object#hasOwnProperty to check if the object has a key:

const hasKeyInList = (obj = {}, keys = []) =>
  keys.some(key => obj.hasOwnProperty(key));

const keys = ['foo', 'bar'];
console.log( hasKeyInList({"foo": "value"}, keys) );
console.log( hasKeyInList({"key": "value"}, keys) );

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!