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

前端 未结 16 2006
余生分开走
余生分开走 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 09:59

    Here is the piece of code that achieves OP's goal using a single function :

    const sources = images.flatMap( (img) =>
        (img.split('.').pop() === "json") ?  [] : [img]
    )
    

    See the developer mozilla documentation of flatMap here

提交回复
热议问题