Find next array index not from start AND if not found loop from beginning

const arr = [{item:'a', value:false}, {item:'b', value:true}, {item:'c', value:true}, {item:'d', value:false}];
const startIndex = 2;

Given startIndex find next object in array with value=true.
Iteration must be forward and, if not found, start from index 0.
Should return arr[1];

I’ve just hit the wall and seems can’t find an iteration method:/

let result = {};
for (let i = startIndex + 1; i < arr.length; i++) {
    if (!arr[i] && i < arr.length) {
        result = arr[i];
    }
}
console.log(result);

4 thoughts on “Find next array index not from start AND if not found loop from beginning”

Leave a Comment