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

188
Visualizações
can i push the index in recursive function?

my recursive array,that i should find last element and the index

let array = [1, [2, "kkkk", [9, "some", ["six", "seven", 6666, [8, "10", [10,["you find me"]]]]]]];

function findIndex(arr) {
    for (let [index, element] of arr.entries()){
        let array = [];
        if(typeof element === "object"){
            findIndex(element);
            array.push(index);
            console.log(array);
        }
    }
}    
findIndex(array)

  1. i add this line let array = []

  2. in console i should have only index like this [1,2,2,3,2], but now i have indexes in different arrays [1][2].....

  3. it must be recursive function.

  4. can i do it with this example ?

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

0

let array = [1, [2, "kkkk", [9, "some", ["six", "seven", 6666, [8, "10", [10, ["you find me"]]]]]]];

const getIndexArray = (array) => {

  const recursiveFnc = (array, acc = []) => {

    // Get index of the last element in a current array
    const lastIndex = array.length - 1;
    // Get the last element in a current array
    const lastElement = array[lastIndex];

    // Push index of last element
    acc.push(lastIndex);

    // If last element is an array go in that array recursively, if not return the last element
    return Array.isArray(lastElement) ? recursiveFnc(lastElement, acc) : acc;
  };

  return recursiveFnc(array);

};

const result = getIndexArray(array);
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