I want to do something that seems simple but I cannot figure it out. I want to use the javascript function 'filter' to find values in an array greater than a value and then return the indices that correspond the those filtered values (or values in that indice range from another array.
arr1 = [1,3,7,9,11];
arr1 = [2,4,8,10,12];
arr1.filter(x => x > 7);
// returns: [9,11]
// desired return: [4,5] (indices from arr1 where values from arr1 that were filtered) and/or [10,12]
// (values from arr2 where values from arr1 that were filtered)
I know the code is not right but I cannot figure out how to get desired result. Any help would be appreciated. Thanks
arr1should bearr2. Given that,arr1.filter(x => arr2[x] > 7);- zero298.filter- the only thing.filterdoes is to reduce the amount of elements in the array, it doesn't transform them. You probably wantreduceor amapto tuples (essentially tag values with their index) ->filterbased on the data ->mapback to indeces. - VLAZfilteraccepts a special type of function called predicate. Return type of apredicateisBooleanby definition which limitsfilterfunctionality to just filter elements. - Xaqron