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?
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]