How to skip over an element in .map()?

前端 未结 16 2071
余生分开走
余生分开走 2020-11-27 09:26

How can I skip an array element in .map?

My code:

var sources = images.map(function (img) {
    if(img.src.split(\'.\').pop() === \"json         


        
16条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 10:11

    var sources = images.map(function (img) {
        if(img.src.split('.').pop() === "json"){ // if extension is .json
            return null; // skip
        }
        else{
            return img.src;
        }
    }).filter(Boolean);
    

    The .filter(Boolean) will filter out any falsey values in a given array, which in your case is the null.

提交回复
热议问题