Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
Checking if object has property with given name returns false

Hi I have this simple code:

let obj = {
    games: [
        {id: 'test'}
    ]
}

console.log(obj.games[0].id) // returns: test
console.log('games[0].id' in obj) // returns: false
console.log(obj.hasOwnProperty('games[0].id')) // returns: false

I don't understand the return value when I try to check for its property name instead of returning its property value.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

in and hasOwnProperty() just take a single property name, they don't accept an expression with nested properties.

So you have to test each level separately.

console.log('games' in obj && 0 in obj.games && "id" in obj.games[0])

You can also use optional chaining to combine all but the last check.

console.log(obj?.games.?[0] && "id" in obj.games[0])
about 4 years ago · Juan Pablo Isaza Relatório

0

The in operator can only test a single attribute. It's not intelligent enough to follow a path.

You could just

const value = obj?.games?.[0]?.id;
const exists = value !== undefined;
console.log(exists)

More importantly, in this case you really shouldn't use the in operator at all. The disadvantage of the in operator, is that it requires the tested field to be coded as a string.

In general putting the names of fields in strings is something to avoid, because it makes it harder to find usages and to refacture code later on.

The use of the in operator can only be justified if the name of the tested field is not a constant, not known in advance.


Having said that, the hasOwnProperty function does something slightly different. It is actually more strict than the solution presented above, and also more strict than the in operator. It strictly checks for fields on the object itself, not the ones of super/base types.

The hasOwnProperty() method returns true if the specified property is a direct property of the object — even if the value is null or undefined. The method returns false if the property is inherited, or has not been declared at all. Unlike the in operator, this method does not check for the specified property in the object's prototype chain. (source: MDN)

about 4 years ago · Juan Pablo Isaza Relatório

0

You have three objects and the methods you are using to test are meant for a single object.

  • obj - object containing a games property
  • obj.games - array with one element
  • obj.games[0] - object with id property

Consider these which all are true:

let obj = {
    games: [
        {id: 'test'}
    ]
}

console.log(`obj.hasOwnProperty('games')`, obj.hasOwnProperty('games'))
console.log(`'games' in obj`, 'games' in obj)

console.log(`obj.games.hasOwnProperty('0')`, obj.games.hasOwnProperty('0'))
console.log(`'0' in obj.games`, '0' in obj.games)

console.log(`obj.games[0].hasOwnProperty('id')`, obj.games[0].hasOwnProperty('id'))
console.log(`'id' in obj.games[0]`, 'id' in obj.games[0])

if (obj && obj.games && obj.games.length > 0 && obj.games[0].id) {
  console.log('obj.games has at least one element, '
       + 'and the first element has an id')
}


let id = obj && obj.games && obj.games.length > 0 && obj.games[0].id;
console.log('id:', id);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda