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

151
Visualizações
Finding the index for which a certain of element of an array exists in another array for last time
arr = ['1', '2', '3', '4', '+', '-', '*', '5', '6']
operatorsValue = ['+', '-', '*', '/', '%']
operatorPosition = arr.findIndex((x) => operatorsValue.includes(x));

Using operatorPosition I can find the index of '+' but i want to find the operator of '*' in the arr.

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

0

There's no built-in findLastIndex method on arrays. Just loop backward through the array looking for a match:

let operatorPosition;
for (let i = arr.length - 1; i >= 0; --i) {
    if (operatorsValue.includes(arr[i])) {
        operatorPosition = i;
        break;
    }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use array#reduce and array#indexOf to create an object with the indexes of all the operators in the array.

let oprObject = operatorsValue.reduce((acc, opr) => {
  acc[opr] = arr.indexOf(opr)
  return acc
}, {})

Demo:

arr = ['1', '2', '3', '4', '+', '-', '*', '5', '6']
operatorsValue = ['+', '-', '*', '/', '%']

let oprObject = operatorsValue.reduce((acc, opr) => {
  acc[opr] = arr.indexOf(opr)
  return acc
}, {})

console.log(oprObject)
console.log("+ is at index:", oprObject["+"])
console.log("* is at index:", oprObject["*"]) 

about 4 years ago · Juan Pablo Isaza Relatório

0

Compute the last index for each operator and take the max of them:

arr = ['1', '2', '3', '4', '+', '-', '*', '5', '6']
operatorsValue = ['+', '-', '*', '/', '%']
lastOperatorPosition = Math.max(...operatorsValue.map(op => arr.lastIndexOf(op)))

console.log(lastOperatorPosition)

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