Given following array:
var arr = [undefined, undefined, 2, 5, undefined, undefined];
I’d like to get the count of elements which are defined (i.e.: those which are not undefined). Other than looping through the array, is there a good way to do this?
In recent browser, you can use filter
Another method, if the browser supports indexOf for arrays:
If for absurd you have one-digit-only elements in the array, you could use that dirty trick:
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.