What is the in-place alternative to Array.prototype.filter()

I’ve got an array that I would like to remove some elements from. I can’t use Array.prototype.filter(), because I want to modify the array in place (because it saves a memory allocation and, more important for me, makes the code more simple in my use case). Is there an in-place alternative to filter that I can use, maybe analogously to how Array.prototype.forEach() can be used as an in-place variant to Array.prototype.map()?

Edit: Minimum example upon request:

  • Make function that supports currying AND the traditional multiple parameters
  • Removing numerous for loops from Javascript code
  • Functional reactive programming — is Fay expressive enough?
  • Is a nested pure function still a pure function?
  • Using assertion library outside of testing for list manipulations
  • Functional Javascript BaconJS, how can I push more values to an event stream?
  • function someCallback(array) {
      // do some stuff
      array.filterInPlace(function(elem) {
        var result = /* some logic */
        return result;
      })
      // do some more stuff
    }
    

  • Is there a name for 'map'ping multiple lists?
  • Correct use of a fold or reduce function to long-to-wide data in python or javascript?
  • Ramda JS: How to perform a map where I call R.replace for a given property on each object?
  • JavaScript: Functional mapping?
  • What is a good example for functional style web programming using javascript?
  • Performance implications of using Functional style Javascript vs “procedural”
  • 2 Solutions collect form web for “What is the in-place alternative to Array.prototype.filter()”

    Is there an in-place alternative to filter

    No, but it’s not hard to write your own. Here is an approach which squeezes out all the values which fail a condition.

    function filterInPlace(a, condition) {
      let i = 0, j = 0;
    
      while (i < a.length) {
        const val = a[i];
        if (condition(val, i, a)) a[j++] = val;
        i++;
      }
    
      a.length = j;
      return a;
    }
    

    condition is designed to have the same signature as the callback passed to Array#filter, namely (value, index, array). For complete compatibility with Array#filter, you could also accept a fourth thisArg parameter.

    Using forEach

    Using forEach has the minor advantage that it will skip empty slots. This version also implements thisArg and does a minor optimization of skipping the assignment if we have not yet encountered a failing element.

    function filterInPlace(a, condition, thisArg) {
      let j = 0, squeezing = false;
    
      a.forEach((e, i) => { 
        if (condition.call(thisArg, e, i, a)) {
          if (squeezing) a[j] = e; 
          j++;
        } else squeezing = true;
      });
    
      a.length = j;
      return a;
    } 
    

    What you could use

    • filter returns an array with the same elements, but not necesserily all.
    • map returns something for each loop, the result is an array with the same length as the source array
    • forEach returns nothing, but every element is processes, like above.
    • reduce returns what ever you want.
    • some/every returns a boolean value

    But nothing from above is mutilating the original array in question of length in situ.

    I suggest to use a while loop, beginning from the last element and apply splice to the element, you want to remove.

    This keeps the index valid and allows to decrement for every loop.

    Example:

    var array = [0, 1, 2, 3, 4, 5],
        i = array.length;
    
    while (i--) {
        if (array[i] % 2) {
            array.splice(i, 1);
        }
    }
    console.log(array);