Count of “Defined” Array Elements

后端 未结 7 1573
无人及你
无人及你 2020-12-03 04:30

Given following array:

var arr = [undefined, undefined, 2, 5, undefined, undefined];

I\'d like to get the count of elements which are

7条回答
  •  一生所求
    2020-12-03 05:21

    If the undefined's are implicit then you can do:

    var len = 0;
    for (var i in arr) { len++ };
    

    undefined's are implicit if you don't set them explicitly

    //both are a[0] and a[3] are explicit undefined
    var arr = [undefined, 1, 2, undefined];
    
    arr[6] = 3;
    //now arr[4] and arr[5] are implicit undefined
    
    delete arr[1]
    //now arr[1] is implicit undefined
    
    arr[2] = undefined
    //now arr[2] is explicit undefined
    

提交回复
热议问题