Count of “Defined” Array Elements

后端 未结 7 1556
无人及你
无人及你 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:03

    No, the only way to know how many elements are not undefined is to loop through and count them. That doesn't mean you have to write the loop, though, just that something, somewhere has to do it. (See #3 below for why I added that caveat.)

    How you loop through and count them is up to you. There are lots of ways:

    1. A standard for loop from 0 to arr.length - 1 (inclusive).
    2. A for..in loop provided you take correct safeguards.
    3. Any of several of the new array features from ECMAScript5 (provided you're using a JavaScript engine that supports them, or you've included an ES5 shim, as they're all shim-able), like some, filter, or reduce, passing in an appropriate function. This is handy not only because you don't have to explicitly write the loop, but because using these features gives the JavaScript engine the opportunity to optimize the loop it does internally in various ways. (Whether it actually does will vary on the engine.)

    ...but it all amounts to looping, either explicitly or (in the case of the new array features) implicitly.

    0 讨论(0)
  • 2020-12-03 05:11

    Remove the values then check (remove null check here if you want)

    const x = A.filter(item => item !== undefined || item !== null).length
    

    With Lodash

    const x = _.size(_.filter(A, item => !_.isNil(item)))
    
    0 讨论(0)
  • 2020-12-03 05:13

    Loop and count in all browsers:

    var cnt = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] !== undefined) {
            ++cnt;
        }
    }
    

    In modern browsers:

    var cnt = 0;
    arr.foreach(function(val) {
        if (val !== undefined) { ++cnt; }
    })
    
    0 讨论(0)
  • 2020-12-03 05:15

    In recent browser, you can use filter

    var size = arr.filter(function(value) { return value !== undefined }).length;
    
    console.log(size);
    

    Another method, if the browser supports indexOf for arrays:

    var size = arr.slice(0).sort().indexOf(undefined);
    

    If for absurd you have one-digit-only elements in the array, you could use that dirty trick:

    console.log(arr.join("").length);
    

    There are several methods you can use, but at the end we have to see if it's really worthy doing these instead of a loop.

    0 讨论(0)
  • 2020-12-03 05:20

    Unfortunately, No. You will you have to go through a loop and count them.

    EDIT :

    var arrLength = arr.filter(Number);
    alert(arrLength);
    

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题