I have a variable, idx, that controls the current index of an array of elements called items. While running a filter on this array, I want to change idx to current index of the filtered array if it matches a condition.
let idx = 5; //6 in the array
let items = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// Filter evens only
let evens = items.filter(function(num) {
if (!isEven(num))
return false;
if (num == 6)
idx = INDEX_IN_EVENS_ARRAY(would be 2 in this case);
return true;
});
Very simple solution using indexOf function.