Removing undefined values from Array

后端 未结 13 1989
死守一世寂寞
死守一世寂寞 2020-12-02 16:52

In certain situations, it may happen that we have undefined or generally falsy values in Array structures. For instance when reading and filling data f

相关标签:
13条回答
  • 2020-12-02 17:32
    var a =  ["3","", "6"];
    var b =  [23,54,56];
    var result = [];
    
    for (var i=0;i<a.length;++i) {
        if (a[i] != "") {
            result[i] = b[i];
        }
    }
    
    result = result.filter(function( element ) {
       return element !== undefined;
    });
    
    console.log(result);
    
    0 讨论(0)
提交回复
热议问题