If it's actually important to do the filtering in-place without creating another array, you have to go sort-of old school and iterate through the array with two indexes, copying values along the way. Every time you hit an element that fails the filter test, you increment one of the indexes but not the other one. At the end of that, you reset the array .length to the trailing index:
function filterInPlace(array, fn) {
let from = 0, to = 0;
while (from < array.length) {
if (fn(array[from])) {
array[to] = array[from];
to++;
}
from++;
}
array.length = to;
}
This has the advantage of being O(n), with just one pass over the array, while the solutions involving .splice() are O(n2).
To do your "range check", you could write another function to create a filter predicate given a minimum and a maximum value:
function rangePredicate(min, max) {
return n => n >= min && n <= max;
}
Then you can pass the return value of that to the filter function:
var arr = [1, 2, 3, ... ];
filterInPlace(arr, rangePredicate(0, 10));
.filter()always creates a new array. Your code doesn't work because the assignment toarrayin the function is an assignment to the parameter and JavaScript is a pass-by-value language..filter()is doesn't like aforloop, This function actually apply the filter on array entities and return a new filtered array, It doesn't make changes on a actual array likeforloop, So when you need to filter any array then you must need to take a variable which holds filtered array result or just useforloop and remove unsatisfied entries from the array which affects on original array elements