JSFiddle - React, Tailwind, and code Playground
JavaScript
const sum = 5;
const items = [1,2,5,3,4,8];
function findByIndex(arr, base){
let listIndex = [];
for (let i=1, total = arr.length; i < total; i++) {
let item = base - i;
let itemIndex = arr.indexOf(item);
let restIndex = arr.indexOf(i);
if (itemIndex > 0 && restIndex >= 0) {
listIndex.push(itemIndex);
listIndex.push(restIndex);
return listIndex;
}
}
}
findByIndex(items, sum); // [4, 0]