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

229
Visualizações
If statements are not working in using javascript or jsx

Having issues to get code block to log to console

Does not work. Nothing is logged to console

var data = {"_id": "615fc3db8b6a6311aa3295ca", "isComplete": false, "remindTime": {"date": "2021-10-08", "time": "12:08:40 am"}, "subtitle": "Fdfdff", "title": "Dfdf"}

if (data.length > 0) {
    console.log('my data', data);
}

WHAT WORKS:

var data = {"_id": "615fc3db8b6a6311aa3295ca", "isComplete": false, "remindTime": {"date": "2021-10-08", "time": "12:08:40 am"}, "subtitle": "Fdfdff", "title": "Dfdf"}

if (data.title) {
    console.log('my data', data);
}

Not sure what's going wrong

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

0

You are testing for a length property on an plain js object that you have not explicitly placed a length property on.

Testing for a length seems like you're expecting it to be an array.

It would be similar to running this in the console, you'll get undefined.

({}).length

If you'd like to log it if it is an object:

if (data) {
    console.log('my data', data);
}

If you'd like to log it if it has any properties on it:

if (Object.keys(data).length) {
    console.log('my data', data);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

in your case - data is not an array, it is an object. You got to use Object.keys(data).length to find no of keys in an object. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

about 4 years ago · Juan Pablo Isaza Relatório

0

Object does not have any length property. If you want to check the existence of the object, just write this:

if (data) {
    // executable code
}

it will check if the object has any property or not.

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