Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Encontrar el índice para el cual cierto elemento de una matriz existe en otra matriz por última vez
arr = ['1', '2', '3', '4', '+', '-', '*', '5', '6'] operatorsValue = ['+', '-', '*', '/', '%'] operatorPosition = arr.findIndex((x) => operatorsValue.includes(x));

Usando operatorPosition puedo encontrar el índice de '+' pero quiero encontrar el operador de '*' en el arr.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

No hay un método findLastIndex en las matrices. Simplemente recorra la matriz hacia atrás en busca de una coincidencia:

 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 Report

0

Puede usar array#reduce y array#indexOf para crear un objeto con los índices de todos los operadores de la matriz.

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

Manifestación:

 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 Report

0

Calcule el último índice para cada operador y tome el máximo de ellos:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!