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

118
Visualizações
nested if statements javascript

I'm trying to do a lookup until i found an especific value; currently using if statements like this but right now its only two levels and i dont need how many if statements will be needed until the conditions meets.

if (newObject.parent.toString() != 1) {
            alert(newObject.shareholder + ' no es 1 Buscaremos sus padres');
            var newObject = parentSearch.search(newObject.parent.toString())[0].item;

            if (newObject.parent.toString() != 1) {

                var newObject = parentSearch.search(newObject.parent.toString())[0].item;

            } else {
                alert(newObject.shareholder + ' Found');

            }
        } else {
            alert(newObject.shareholder + ' Found');

        }

Is there a way to avoid using infinite IF statements ?

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

0

You can make use of recursion as follow:

function checkObject(newObject) {
    if (newObject.parent.toString() != 1) {
        alert(newObject.shareholder + ' no es 1 Buscaremos sus padres');
        const newObject = parentSearch.search(newObject.parent.toString())[0].item;

        checkObject(newObject)  // recursion
    } else {
        alert(newObject.shareholder + ' Found');

    }
}

In case you cannot use recursion, you can use following:

function checkObject(newObject) { 
    while (true){
        if (newObject.parent.toString() != 1) {
            alert(newObject.shareholder + ' no es 1 Buscaremos sus padres');
            
            newObject = parentSearch.search(newObject.parent.toString())[0].item;
        } else {
            alert(newObject.shareholder + ' Found');
            break;
        }
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You may want to check JSONPath for that deep lookup : https://jsonpath.com/.

It allows you to make a string query and execute it on an object and returns fields that match the query.

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