lodashのlastIndexOfを作成してみた
const lastIndexOf = (array, selectNum, startIndex = array.length - 1) => {
for (let i = startIndex; 0 <= i; i--) {
if (array[i] === selectNum) {
return i
}
}
return -1
}
console.log(lastIndexOf([1, 2, 1, 2], 2))
// => 3
console.log(lastIndexOf([1, 2, 1, 2], 5))
// => -1
console.log(lastIndexOf([1, 2, 1, 2], 2, 2))
//2番目から『2』の値が最初に一致したindexを返す
// => 1