Array.any 是什么?用于 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28350292/
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-08-23 01:38:59  来源:igfitidea点击:

what is Array.any? for javascript

javascript

提问by kangkyu

I'm looking for a method for javascript returns true or false when it's empty.. something like Ruby any?or empty?

我正在寻找一种方法,当它为空时,javascript 返回 true 或 false .. 像 Rubyany?empty?

[].any? #=> false
[].empty? #=> true

采纳答案by georg

JavaScript has the Array.prototype.some()method:

JavaScript 有这样的Array.prototype.some()方法:

[1, 2, 3].some((num) => num % 2 === 0);  

returns truebecause there's (at least) one even number in the array.

返回true是因为数组中有(至少)一个偶数。

In general, the Arrayclassin JavaScript's standard library is quite poor compared to Ruby's Enumerable. There's no isEmptymethod and .some()requires that you pass in a function or you'll get an undefined is not a functionerror. You can define your own .isEmpty()as well as a .any()that is closer to Ruby's like this:

一般来说,与 Ruby 的Enumerable相比,JavaScript 标准库中的Array相当糟糕。没有方法,需要你传入一个函数,否则你会得到一个错误。您可以定义自己的以及更接近 Ruby 的,如下所示:isEmpty.some()undefined is not a function.isEmpty().any()

Array.prototype.isEmpty = function() {
    return this.length === 0;
}

Array.prototype.any = function(func) {
   return this.some(func || function(x) { return x });
}

Libraries like underscore.jsand lodashprovide helper methods like these, if you're used to Ruby's collection methods, it might make sense to include them in your project.

underscore.jslodash这样的库提供了这样的辅助方法,如果你习惯了 Ruby 的集合方法,那么将它们包含在你的项目中可能是有意义的。

回答by asiniy

The JavaScript native .some()methoddoes exactly what you're looking for:

JavaScript 本机.some()方法完全符合您的要求:

function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

回答by Dudo

I'm a little late to the party, but...

我参加聚会有点晚了,但是...

[].some(x => !!x)

回答by TGH

var a = [];
a.length > 0

I would just check the length. You could potentially wrap it in a helper method if you like.

我只是检查长度。如果您愿意,您可以将其包装在辅助方法中。

回答by Pointy

JavaScript arrays can be "empty", in a sense, even if the length of the array is non-zero. For example:

JavaScript 数组在某种意义上可以是“空的”,即使数组的长度不为零。例如:

var empty = new Array(10);
var howMany = empty.reduce(function(count, e) { return count + 1; }, 0);

The variable "howMany" will be set to 0, even though the array was initialized to have a length of 10.

变量“howMany”将被设置为0,即使数组被初始化为长度为10

Thus because many of the Array iteration functions only pay attention to elements of the array that have actually been assigned values, you can use something like this call to .some()to see if an array has anything actually in it:

因此,由于许多 Array 迭代函数只关注实际已分配值的数组元素,因此您可以使用类似此调用的方法.some()来查看数组中是否实际包含任何内容:

var hasSome = empty.some(function(e) { return true; });

The callback passed to .some()will return truewhenever it's called, so if the iteration mechanism finds an element of the array that's worthy of inspection, the result will be true.

传递给的回调.some()true在每次调用时返回,因此如果迭代机制找到值得检查的数组元素,则结果将为true.

回答by Pedro Coelho

I would go with the cleanest, readable and elegant option, which is:

我会选择最干净、易读和优雅的选项,即:

var empty = [];
empty.some(x => x); //returns false

回答by Andy

If you really want to got nuts, add a new method to the prototype:

如果您真的想疯了,请在原型中添加一个新方法:

if (!('empty' in Array.prototype)) {
  Array.prototype.empty = function () {
    return this.length === 0;
  };
}

[1, 2].empty() // false
[].empty() // true

DEMO

演示

回答by Benoit Esnard

Array has a length property:

数组有一个长度属性

[].length // 0
[0].length // 1
[4, 8, 15, 16, 23, 42].length // 6

回答by haim770

Just use Array.length:

只需使用Array.length

var arr = [];

if (arr.length)
   console.log('not empty');
else
   console.log('empty');

See MDN

MDN

回答by Abdennour TOUMI

What you want is .emptynot .empty()to fully mimics Ruby :

你想要的.empty不是.empty()完全模仿 Ruby :

     Object.defineProperty( Array.prototype, 'empty', {
           get: function ( ) { return this.length===0 }
      } );    

then

然后

[].empty //true
[3,2,8].empty //false


For any, see my answer here

对于任何人,请在此处查看我的答案