Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count empty values in array

Tags:

javascript

Given an array:

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

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

var empties = arr.length - arr.filter(function(x){ return true }).length;

// return 3

or something like this

arr.empties = arr.length;
arr.forEach(function(x){ arr.empties--  });

// arr.empties returns 3

Is this the best way or am I missing something?

like image 536
vsync Avatar asked Jun 07 '11 13:06

vsync


People also ask

How do you know if an array is empty?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

Can elements of array be empty?

An array value can be non-empty, empty (cardinality zero), or null. The individual elements in the array can be null or not null.

How do you empty an array?

Input : Array = [2, 3, 5, 4, 1] Output : Steps Taken: 3 Explanation: Step 1: Remove 5 and elements to its right so, Array becomes [2, 3] Step 2: Remove 3 as it is the maximum and right most already so, Array becomes [2] Step 3: Remove 2 and the array becomes EMPTY Hence, at the end of step 3 the array stands exhausted.


2 Answers

Based on your comments to another answer, it looks like you're after the shortest method. Well, you might want to consider a variation of your own example:

var empties = arr.length - arr.filter(String).length;

All you're doing is passing a native function rather than an anonymous function, saving a few precious bytes. Any native constructor or function will do, as long as it doesn't return a boolean.


You need to be more specific about what you would consider the 'best way'. For instance, some methods will give better performance than others, some are more concise and some have better compatibility.

The solutions you mention in the post require browsers to be compatible with the ECMAScript 5th Edition specification, so they won't work in some older browsers (read: IE8 and lower).

The "best" all-round approach is a simple loop. It's not as concise as your methods, but it will no doubt be the fastest and most compatible:

var arr = [1,,2,5,6,,4,5,6,,], count = 0, i = arr.length;

while (i--) {
    if (typeof arr[i] === "undefined")
        count++;
}

This makes use of loop optimisations (using while and decrementing is faster than for).

Another approach would be to sort the array so that undefined items are all at the end and use a loop to iterate backwards:

var arr = [1,,2,5,6,,4,5,6,,], count = 0;
arr.sort();
while (typeof arr.pop() === "undefined") count++;

alert(count); 
//-> 3

This approach would modify the original array and remove those items which may not be what you want. However, it may be much faster on very large arrays.

Performance test suite
http://jsperf.com/count-undefined-array-elements

like image 73
Andy E Avatar answered Sep 19 '22 19:09

Andy E


Looks good to me.

You could also do:

var empties = arr.reduce(function(x, y){ return x-1; }, arr.length);

Also, if you don't mind sorting the array, you might get a little added performance out of:

arr.sort();
for (var j=arr.length-1; j > 0 && arr[j] === undefined; j--) {}
var empties = arr.length-j-1;
like image 30
Brett Zamir Avatar answered Sep 20 '22 19:09

Brett Zamir