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

139
Visualizações
I want to print index of all elements without using forEach or filter or map

Here is my code it is giving me the same index number of the element.

I want it like this 0,1,2,3,4,5,6

let as=[1, 2, 3, 4, 5, 5,5]

for(let i=0;i<as.length;i++){
  console.log(as[i], as.indexOf(as[i]))
}

Here is my output :

1 0
2 1
3 2
4 3
5 4
5 4
5 4
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

If I understand your question correctly, you want to fetch the index of the elements available in an array. If Yes, Then you can do like this :

let as = [1, 2, 3, 4, 5, 5,5];

const indexArray = as.map((item, index) => index);

console.log(indexArray.join(','));

about 4 years ago · Juan Pablo Isaza Relatório

0

Create an array you can push the indexes into, and then create a string with join.

const as = [1, 2, 3, 4, 5, 5, 5];
let out = [];

for (let i = 0; i < as.length; i++) {
  out.push(i);
}

console.log(out.join(','));

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

let as=[1, 2, 3, 4, 5, 5,5]

for(let i=0;i<as.length;i++){
  console.log(as[i], as.indexOf(as[i],i))
}

Explaination: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

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