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

105
Views
Test if object contains property

I am trying to check for an object property, but I can't understand why the second option returns false. Could anyone explain? Also, are there any other better ways to check properties?

let question = {
    category: 'test'
}

console.log(question.hasOwnProperty('category')); // true

this wont work

let question = {
    category: 'test'
}

console.log(question.hasOwnProperty(question.category)); // false
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const obj = {
    foo: 'bar',
};

// Check if an object has a certain key somewhere
console.log('foo' in obj);
console.log(!!obj['foo']);
console.log(obj.hasOwnProperty('foo'))

// Check if an object has a certain value somewhere
console.log(Object.values(obj).includes('bar'));

about 4 years ago · Juan Pablo Isaza Report

0

In this line:

question.hasOwnProperty(question.category)

The part question.category returns 'test' and you haven't 'test' like property, just leave it like that:

question.hasOwnProperty(category)

about 4 years ago · Juan Pablo Isaza Report

0

You can also check property using:

'category' in question
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!