I currently have a variable that contains an array of numbers
let numbersArray = [12, 15, 19, 20];
When looping over it I want to delete the entry if it is a certain value
let numbersArray = [12,15,19,20],
filteredNumbersArray = numbersArray.map(function(value)
{
if(value === 15)
{
//delete the value
} else {
return value * 2;
}
});
In regards to using .filter()
, the value must be mutatable, I have updated the example
I have searched through MDN but couldn't find anything about removing an element within a map()
.
The Rubber Duck didn't help either
I currently have a variable that contains an array of numbers
let numbersArray = [12, 15, 19, 20];
When looping over it I want to delete the entry if it is a certain value
let numbersArray = [12,15,19,20],
filteredNumbersArray = numbersArray.map(function(value)
{
if(value === 15)
{
//delete the value
} else {
return value * 2;
}
});
In regards to using .filter()
, the value must be mutatable, I have updated the example
I have searched through MDN but couldn't find anything about removing an element within a map()
.
The Rubber Duck didn't help either
Share Improve this question edited Apr 1, 2018 at 16:02 Ian asked Apr 1, 2018 at 15:58 IanIan 3,6764 gold badges30 silver badges50 bronze badges 3 |3 Answers
Reset to default 9You could filter and then map
var array = [12, 15, 19, 20],
result = array
.filter(v => v !== 15)
.map(v => 2 * v);
console.log(result);
Or use the swiss knife of all array manipulations: Array#reduce
var array = [12, 15, 19, 20],
result = array.reduce((r, v) => v !== 15 ? r.concat(v): r, []);
console.log(result);
With some optimizations
var array = [12, 15, 19, 20],
result = array.reduce(function (r, v) {
if (v !== 15) {
r.push(2 * v);
}
return r;
}, []);
console.log(result);
map
can't remove values. If you need to simultaneously map
and filter
, your options are:
filter
, thenmap
, e.g.:filteredNumbersArray = numbersArray.filter(function(v) { return v !== 15; }) .map(function(v) { return v * 2; });
or with ES2015+ syntax:
filteredNumbersArray = numbersArray.filter(v => v !== 15) .map(v => v * 2);
or
Write your own thing, probably:
filteredNumbersArray = []; numbersArray.forEach(function(v) { if (v !== 15) { filteredNumbersArray.push(v * 2); } });
or with ES2015+ syntax:
filteredNumbersArray = []; for (const v of numbersArray) { if (v !== 15) { filteredNumbersArray.push(v * 2); } }
(You can also [ab]use
reduce
here, but it doesn't gain you anything worth having, and costs clarity.)If it comes up a lot for you, give yourself a combined
map-and-filter
function that uses some special return value to indicate that the entry should be dropped.
Map doesn't remove values it just mutates them. You could set the value to null but that is probably not your intent.
Filter might server you better
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
let numbersArray = [12,15,19,20],
filteredNumbersArray = numbersArray.filter(function(value)
{
return value != 15
});
This would return an array without the number 15.
Array#filter
can remove an item,Array#map
returns a value for each item. – Nina Scholz Commented Apr 1, 2018 at 15:59