count empty values in array

后端 未结 3 966
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 05:17

Given an array:

var arr = [1,,2,5,6,,4,5,6,,];

Count how many empty values is has: (length - length after removing the emp

3条回答
  •  一个人的身影
    2020-12-10 05:32

    Or, without using js 1.6 filter/foreach, you could cycle it yourself this way:

    var arr = [1,,2,5,6,,4,5,6,,];
    var emptyElems = 0;
    for(var i=0, l = arr.length; i < l; i++){
        emptyLength += (arr[i] === undefined) ? 1 : 0;
    }
    
    alert(emptyElems); //alerts 3
    

提交回复
热议问题