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

150
Views
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 answers
Answer question

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 Report

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 Report

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 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!