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

151
Visualizações
How to check for the existence of dynamic property inside an object?
const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}
const result = {
    "MEN": [],
    "WOMEN": [],
    "CHILDREN TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

I have an object result like above. Sometimes, it may have KIDS TEMP property and sometimes CHILDREN TEMP. Since this property has spaces, I cannot use dot operator to fetch these properties. I want to create a flag if either of KIDS TEMP or CHILDREN TEMP exist. I tried below:-

const part = 'KIDS TEMP' || 'CHILDREN TEMP';
    let check;
    if (result) {
      check = !!(result?.MEN.length !== 0 || result[part].length !== 0);
    }

But the above code breaks when I receive CHILDREN TEMP saying that it cannot read length of undefined. How do I fix this?

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

0

Object.keys() returns the property names of an object. You can check like this, if an object possesses a certain property:

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(Object.keys(result).includes("KIDS TEMP"));
console.log(Object.keys(result).includes("CHILDREN TEMP"));

If you want to test, if a certain attribute exists, and in case it exists, contains a non-empty array, you can test it like this:

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(result["KIDS TEMP"]?.length > 0);
console.log(result["CHILDREN TEMP"]?.length > 0);

The main problem with your code is that const part = string1 || string2 always evaluates to string1, which is not what you intend to do.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Object's method hasOwnProperty it will return true/false depends on if property exists in object or not.

const result = {
    "MEN": [],
    "WOMEN": [],
    "KIDS TEMP": [
        "BCDEFHJJJJJJ34EEE234",
        "ABCDEFGHIJKL12345678"
    ]
}

console.log(result.hasOwnProperty('KIDS TEMP'),result.hasOwnProperty('CHILDREN TEMP'));

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