Removing elements with Array.map in JavaScript
Asked Answered
P

8

133

I would like to filter an array of items by using the map() function. Here is a code snippet:

var filteredItems = items.map(function(item)
{
    if( ...some condition... )
    {
        return item;
    }
});

The problem is that filtered out items still uses space in the array and I would like to completely wipe them out.

My specific code was not intended to run in a browser, so I'm not worried about browser-specific support for either function.

Any idea?

Peeler answered 12/8, 2008 at 22:21 Comment(2)
Can you elaborate on why 2 iterations are worst that 1 ? I mean, 2*O(n) is equivalent to O(2*n) to me...Peeler
It's often useful to filter and map in one function, not for performance reasons, but rather to reduce duplicate code. In fact, Swift has this built in with it's compactMap function. See this answer for more info.Annia
P
138

You should use the filter method rather than map unless you want to mutate the items in the array, in addition to filtering.

eg.

var filteredItems = items.filter(function(item)
{
    return ...some condition...;
});

[Edit: Of course you could always do sourceArray.filter(...).map(...) to both filter and mutate]

Prognathous answered 12/8, 2008 at 22:38 Comment(8)
map doesn't mutateVocoid
But you can mutate in map.Catapult
Careful with this: as JS passes reference when you mutate something with map it will change the object but as MDN stands, maps returns the mutated array.Behnken
@Behnken No, map does not mutate, and does not return a mutated array. It returns a new array. e.g., x=[1,2,3];y = x.map(z => z*2);console.log(x,y);Ballesteros
But can you mutate some AND remove some with .map?Fucoid
I'd actually recommend doing map and then filter. See this answer for more info.Annia
Not correct. The filter method constructs a dense array, not preserving the index! [0,1,2,,,,,0,,0,,0,,,,,3].reduce((r,x,i) => r+(i>0?",":"")+i+":"+x, "[")+"]" gives "[0:0,1:1,2:2,7:0,9:0,11:0,16:3]" while [0,1,2,,,,,0,,0,,0,,,,,3].filter((x) => true).reduce((r,x,i) => r+(i>0?",":"")+i+":"+x, "[")+"]" gives "[0:0,1:1,2:2,3:0,4:0,5:0,6:3]"Springclean
Do I have to return true or false to keep it in?Photometry
B
66

Inspired by writing this answer, I ended up later expanding and writing a blog post going over this in careful detail. I recommend checking that out if you want to develop a deeper understanding of how to think about this problem--I try to explain it piece by piece, and also give a JSperf comparison at the end, going over speed considerations.

That said, **The tl;dr is this:

To accomplish what you're asking for (filtering and mapping within one function call), you would use Array.reduce()**.

However, the more readable and (less importantly) usually significantly faster2 approach is to just use filter and map chained together:

[1,2,3].filter(num => num > 2).map(num => num * 2)

What follows is a description of how Array.reduce() works, and how it can be used to accomplish filter and map in one iteration. Again, if this is too condensed, I highly recommend seeing the blog post linked above, which is a much more friendly intro with clear examples and progression.


You give reduce an argument that is a (usually anonymous) function.

That anonymous function takes two parameters--one (like the anonymous functions passed in to map/filter/forEach) is the iteratee to be operated on. There is another argument for the anonymous function passed to reduce, however, that those functions do not accept, and that is the value that will be passed along between function calls, often referred to as the memo.

Note that while Array.filter() takes only one argument (a function), Array.reduce() also takes an important (though optional) second argument: an initial value for 'memo' that will be passed into that anonymous function as its first argument, and subsequently can be mutated and passed along between function calls. (If it is not supplied, then 'memo' in the first anonymous function call will by default be the first iteratee, and the 'iteratee' argument will actually be the second value in the array)

In our case, we'll pass in an empty array to start, and then choose whether to inject our iteratee into our array or not based on our function--this is the filtering process.

Finally, we'll return our 'array in progress' on each anonymous function call, and reduce will take that return value and pass it as an argument (called memo) to its next function call.

This allows filter and map to happen in one iteration, cutting down our number of required iterations in half--just doing twice as much work each iteration, though, so nothing is really saved other than function calls, which are not so expensive in javascript.

For a more complete explanation, refer to MDN docs (or to my post referenced at the beginning of this answer).

Basic example of a Reduce call:

let array = [1,2,3];
const initialMemo = [];

array = array.reduce((memo, iteratee) => {
    // if condition is our filter
    if (iteratee > 1) {
        // what happens inside the filter is the map
        memo.push(iteratee * 2); 
    }

    // this return value will be passed in as the 'memo' argument
    // to the next call of this function, and this function will have
    // every element passed into it at some point.
    return memo; 
}, initialMemo)

console.log(array) // [4,6], equivalent to [(2 * 2), (3 * 2)]

more succinct version:

[1,2,3].reduce((memo, value) => value > 1 ? memo.concat(value * 2) : memo, [])

Notice that the first iteratee was not greater than one, and so was filtered. Also note the initialMemo, named just to make its existence clear and draw attention to it. Once again, it is passed in as 'memo' to the first anonymous function call, and then the returned value of the anonymous function is passed in as the 'memo' argument to the next function.

Another example of the classic use case for memo would be returning the smallest or largest number in an array. Example:

[7,4,1,99,57,2,1,100].reduce((memo, val) => memo > val ? memo : val)
// ^this would return the largest number in the list.

An example of how to write your own reduce function (this often helps understanding functions like these, I find):

test_arr = [];

// we accept an anonymous function, and an optional 'initial memo' value.
test_arr.my_reducer = function(reduceFunc, initialMemo) {
    // if we did not pass in a second argument, then our first memo value 
    // will be whatever is in index zero. (Otherwise, it will 
    // be that second argument.)
    const initialMemoIsIndexZero = arguments.length < 2;

    // here we use that logic to set the memo value accordingly.
    let memo = initialMemoIsIndexZero ? this[0] : initialMemo;

    // here we use that same boolean to decide whether the first
    // value we pass in as iteratee is either the first or second
    // element
    const initialIteratee = initialMemoIsIndexZero ? 1 : 0;

    for (var i = initialIteratee; i < this.length; i++) {
        // memo is either the argument passed in above, or the 
        // first item in the list. initialIteratee is either the
        // first item in the list, or the second item in the list.
           memo = reduceFunc(memo, this[i]);
        // or, more technically complete, give access to base array
        // and index to the reducer as well:
        // memo = reduceFunc(memo, this[i], i, this);
    }

    // after we've compressed the array into a single value,
    // we return it.
    return memo;
}

The real implementation allows access to things like the index, for example, but I hope this helps you get an uncomplicated feel for the gist of it.

Ballesteros answered 11/4, 2016 at 19:25 Comment(1)
Another usefulness of reduce is that, unlike filter + map, the callback can be passed an index argument that is the index of the original array, and not that of the filtered one.Swoosh
P
13

That's not what map does. You really want Array.filter. Or if you really want to remove the elements from the original list, you're going to need to do it imperatively with a for loop.

Propagandize answered 12/8, 2008 at 22:33 Comment(0)
S
7

Array Filter method

var arr = [1, 2, 3]

// ES5 syntax
arr = arr.filter(function(item){ return item != 3 })

// ES2015 syntax
arr = arr.filter(item => item != 3)

console.log( arr )
Sexist answered 30/9, 2009 at 14:56 Comment(3)
you can also do var arr = [1,2,"xxx", "yyy"]; arr = arr.filter(function(e){ return e!="xxx" }) console.log(arr)Acridine
You came back 4 years later to add huge text? minus oneVocoid
@user633183 Who are you referring? what "huge text"? Your comment is unclear. Are you sure you're commenting on the right place...?Sexist
C
1

You must note however that the Array.filter is not supported in all browser so, you must to prototyped:

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.filter)
{
    Array.prototype.filter = function(fun /*, thisp*/)
    {
        var len = this.length;

        if (typeof fun != "function")
            throw new TypeError();

        var res = new Array();
        var thisp = arguments[1];

        for (var i = 0; i < len; i++)
        {
            if (i in this)
            {
                var val = this[i]; // in case fun mutates this

                if (fun.call(thisp, val, i, this))
                   res.push(val);
            }
        }

        return res;
    };
}

And doing so, you can prototype any method you may need.

Churchman answered 12/8, 2008 at 23:44 Comment(1)
If you really intend to polyfill this method, please use a proper polyfill, or better yet a library like Modernizr. Otherwise, you'll likely run into confusing bugs with obscure browsers that you won't realize until they've been in production for too long.Ballesteros
A
0

TLDR: Use map (returning undefined when needed) and then filter.


First, I believe that a map + filter function is useful since you don't want to repeat a computation in both. Swift originally called this function flatMap but then renamed it to compactMap.

For example, if we don't have a compactMap function, we might end up with computation defined twice:

  let array = [1, 2, 3, 4, 5, 6, 7, 8];
  let mapped = array
  .filter(x => {
    let computation = x / 2 + 1;
    let isIncluded = computation % 2 === 0;
    return isIncluded;
  })
  .map(x => {
    let computation = x / 2 + 1;
    return `${x} is included because ${computation} is even`
  })

  // Output: [2 is included because 2 is even, 6 is included because 4 is even]

Thus compactMap would be useful to reduce duplicate code.

A really simple way to do something similar to compactMap is to:

  1. Map on real values or undefined.
  2. Filter out all the undefined values.

This of course relies on you never needing to return undefined values as part of your original map function.

Example:

  let array = [1, 2, 3, 4, 5, 6, 7, 8];
  let mapped = array
  .map(x => {
    let computation = x / 2 + 1;
    let isIncluded = computation % 2 === 0;
    if (isIncluded) {
      return `${x} is included because ${computation} is even`
    } else {
      return undefined
    }
  })
  .filter(x => typeof x !== "undefined")
Annia answered 19/2, 2021 at 18:10 Comment(1)
or you could just use reduce to prevent repeat computation.Ballesteros
L
-1

I just wrote array intersection that correctly handles also duplicates

https://gist.github.com/gkucmierz/8ee04544fa842411f7553ef66ac2fcf0

// array intersection that correctly handles also duplicates

const intersection = (a1, a2) => {
  const cnt = new Map();
  a2.map(el => cnt[el] = el in cnt ? cnt[el] + 1 : 1);
  return a1.filter(el => el in cnt && 0 < cnt[el]--);
};

const l = console.log;
l(intersection('1234'.split``, '3456'.split``)); // [ '3', '4' ]
l(intersection('12344'.split``, '3456'.split``)); // [ '3', '4' ]
l(intersection('1234'.split``, '33456'.split``)); // [ '3', '4' ]
l(intersection('12334'.split``, '33456'.split``)); // [ '3', '3', '4' ]
Lobelia answered 15/11, 2019 at 15:29 Comment(0)
Y
-1

First you can use map and with chaining you can use filter

state.map(item => {
            if(item.id === action.item.id){   
                    return {
                        id : action.item.id,
                        name : item.name,
                        price: item.price,
                        quantity : item.quantity-1
                    }

            }else{
                return item;
            }
        }).filter(item => {
            if(item.quantity <= 0){
                return false;
            }else{
                return true;
            }
        });
Yokefellow answered 20/4, 2020 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.