Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

You can also check property using:

'category' in question
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda