javascript 计算数组中的空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6265940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:05:29  来源:igfitidea点击:

count empty values in array

javascript

提问by vsync

Given an array:

给定一个数组:

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

Count how many emptyvalues 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?

这是最好的方法还是我错过了什么?

回答by Andy E

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).

您在帖子中提到的解决方案要求浏览器与 ECMAScript 第 5 版规范兼容,因此它们在某些较旧的浏览器(阅读:IE8 及更低版本)中不起作用。

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 whileand decrementing is faster than for).

这利用了循环优化(使用while和递减比 快for)。

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

另一种方法是对数组进行排序,以便undefined项目都在最后,并使用循环向后迭代:

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 maybe much faster on very large arrays.

这种方法会修改原始数组并删除那些可能不是您想要的项目。但是,在非常大的阵列上它可能会快得多。

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

性能测试套件
http://jsperf.com/count-undefined-array-elements

回答by Brett Zamir

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;

回答by stecb

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

或者,不使用 js 1.6 filter/foreach,你可以这样自己循环:

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