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

101
Visualizações
Check if object haves any nested objects and get its value

I have a sequence of JSON objects coming from a post request

{
  "labels": {
     "name": "Toma/Dyrskuvegen5/360.001/Avkasttemperatur"
  }
}

In some sequences the labels are empty (in which case should return false), in some, they are not. Also in some sequences the

labels.name

will be some other random key name, for example:

labels.room_number

My task is to identify if there is any key under labels and set two strings, one with the key name, and the other with the key-value example from above:

n = "name"

v = "Toma/Dyrskuvegen5/360.001/Avkasttemperatur"

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

0

You can use Object.entries to do this. It returns an array of a given object's [key, value] pairs

let incoming = {"labels":
    {"name":
    "Toma/Dyrskuvegen5/360.001/Avkasttemperatur"}};
    let incoming2 = {"labels":
    {"age":
    "22"}};
     let incoming3 = {"labels":
    {}};
    
let getThis = (incoming) => {
  let entries = Object.entries(incoming.labels);
  if(entries.length > 0){
  [s,v] = Object.entries(incoming.labels)[0];
  console.log(s,v);
  }
  return false;
};
getThis(incoming3);
getThis(incoming);
getThis(incoming2);

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