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

270
Visualizações
Getting minimum index while ignoring element value in array

Say we have this array:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

And we have this conditional:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

for(let i = 0; i < arr.length; i++){
    if (arr[i][0] === 3 && arr[i][1] === 4 || arr[i][0] === 3 && arr[i][1] === 5){
        console.log(i)
    }
}

There respective indexes would be 1 and 2, which would display in the console.log() in sequential order.

How would I grab the minimum index (1 in this case), specifically, from that? I cannot store the numbers in an array due to my use case, so I need to establish the minimum within the loop. And you can't really set a min number because you won't know which element index will be the smallest beforehand.

Is this possible or am I overthinking it hard?

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

0

You can declare a variable outside the loop, then assign i to it and break the loop if the condition is true:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

var ind = 0

for(let i = 0; i < arr.length; i++){
    if (arr[i][0] === 3 && arr[i][1] === 4 || arr[i][0] === 3 && arr[i][1] === 5){
        ind = i;
        break;
    }
}

console.log(ind)

Alternatively, you can use Array.findIndex:

arr = [[3, 3], [3, 4], [3, 5], [3, 6]]

var ind = arr.findIndex(e => e[0] === 3 && e[1] === 4 || e[0] === 3 && e[1] === 5)

console.log(ind)

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