snippetjavascriptCritical
How to skip over an element in .map()?
Viewed 0 times
howoverelementskipmap
Problem
How can I skip an array element in
My code:
This will return:
.map?My code:
var sources = images.map(function (img) {
if(img.src.split('.').pop() === "json"){ // if extension is .json
return null; // skip
}
else{
return img.src;
}
});This will return:
["img.png", null, "img.png"]Solution
Just
If you don't want to do that, which is not unreasonable since it has some cost, you can use the more general
can be written as
So if you need to skip elements, you can do that easily with
In that version, the code in the
update — This question gets a lot of attention, and I'd like to add the following clarifying remark. The purpose of
I'm not saying that it doesn't make sense to create a new list from an old list with some values excluded. I'm just trying to make clear that
.filter() it first:var sources = images.filter(function(img) {
if (img.src.split('.').pop() === "json") {
return false; // skip
}
return true;
}).map(function(img) { return img.src; });If you don't want to do that, which is not unreasonable since it has some cost, you can use the more general
.reduce(). You can generally express .map() in terms of .reduce:someArray.map(function(element) {
return transform(element);
});can be written as
someArray.reduce(function(result, element) {
result.push(transform(element));
return result;
}, []);So if you need to skip elements, you can do that easily with
.reduce():var sources = images.reduce(function(result, img) {
if (img.src.split('.').pop() !== "json") {
result.push(img.src);
}
return result;
}, []);In that version, the code in the
.filter() from the first sample is part of the .reduce() callback. The image source is only pushed onto the result array in the case where the filter operation would have kept it.update — This question gets a lot of attention, and I'd like to add the following clarifying remark. The purpose of
.map(), as a concept, is to do exactly what "map" means: transform a list of values into another list of values according to certain rules. Just as a paper map of some country would seem weird if a couple of cities were completely missing, a mapping from one list to another only really makes sense when there's a 1 to 1 set of result values.I'm not saying that it doesn't make sense to create a new list from an old list with some values excluded. I'm just trying to make clear that
.map() has a single simple intention, which is to create a new array of the same length as an old array, only with values formed by a transformation of the old values.Code Snippets
var sources = images.filter(function(img) {
if (img.src.split('.').pop() === "json") {
return false; // skip
}
return true;
}).map(function(img) { return img.src; });someArray.map(function(element) {
return transform(element);
});someArray.reduce(function(result, element) {
result.push(transform(element));
return result;
}, []);var sources = images.reduce(function(result, img) {
if (img.src.split('.').pop() !== "json") {
result.push(img.src);
}
return result;
}, []);Context
Stack Overflow Q#24806772, score: 1289
Revisions (0)
No revisions yet.