Is there an equivalent of IEnumerable.Any(Predicate<T>)
in JavaScript or jQuery?
在JavaScript或jQuery中是否存在等价于IEnumerable.Any(谓词
I am validating a list of items, and want to break early if error is detected. I could do it using $.each
, but I need to use an external flag to see if the item was actually found:
我正在验证一个项目列表,如果检测到错误,我希望尽早中断。我可以用$。每个,但是我需要使用一个外部标志来查看项目是否被发现:
var found = false;
$.each(array, function(i) {
if (notValid(array[i])) {
found = true;
}
return !found;
});
What would be a better way? I don't like using plain for
with JavaScript arrays because it iterates over all of its members, not just values.
还有什么更好的方法呢?我不喜欢在JavaScript数组中使用plain,因为它遍历所有成员,而不仅仅是值。
7 个解决方案
#1
52
These days you could actually use Array.prototype.some
(specced in ES5) to get the same effect:
现在你可以使用Array.prototype了。有些(见ES5)会得到同样的效果:
array.some(function(item) {
return notValid(item);
});
#2
18
You could use variant of jQuery is
function which accepts a predicate:
您可以使用jQuery is函数的变体,它接受谓词:
$(array).is(function(index) {
return notValid(this);
});
#3
4
You should use an ordinary for
loop (not for ... in
), which will only loop through array elements.
你应该使用一个普通的for循环(不是for…)在)中,它只会循环遍历数组元素。
#4
4
Xion's answer is correct. To expand upon his answer:
Xion的回答是正确的。进一步阐述他的回答:
jQuery's .is(function)
has the same behavior as .NET's IEnumerable.Any(Predicate<T>)
.
jQuery的.is(函数)与。net的IEnumerable.Any(谓词
From http://docs.jquery.com/is:
从http://docs.jquery.com/is:
Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
如果选择中至少有一个元素符合给定的表达式,则根据表达式检查当前选择并返回true。
#5
1
I suggest you to use the $.grep()
method. It's very close to IEnumerable.Any(Predicate<T>)
:
我建议您使用$.grep()方法。它非常接近于IEnumerable.Any(谓词
$.grep(array, function(n, i) {
return (n == 5);
});
Here a working sample to you: http://jsfiddle.net/ErickPetru/BYjcu/.
这里有一个工作示例:http://jsfiddle.net/ErickPetru/BYjcu/。
#6
1
I would suggest that you try the JavaScript for in
loop. However, be aware that the syntax is quite different than what you get with a .net IEnumerable
. Here is a small illustrative code sample.
我建议您尝试用JavaScript进行循环。但是,请注意,语法与.net IEnumerable是完全不同的。这是一个小型的说明性代码示例。
var names = ['Alice','Bob','Charlie','David'];
for (x in names)
{
var name = names[x];
alert('Hello, ' + name);
}
var cards = { HoleCard: 'Ace of Spades', VisibleCard='Five of Hearts' };
for (x in cards)
{
var position = x;
var card = card[x];
alert('I have a card: ' + position + ': ' + card);
}
#7
0
You might use array.filter (IE 9+ see link below for more detail)
您可以使用数组。过滤器(ie9 +参见下面的链接以获得更详细的信息)
[].filter(function(){ return true|false ;}).length > 0;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
#1
52
These days you could actually use Array.prototype.some
(specced in ES5) to get the same effect:
现在你可以使用Array.prototype了。有些(见ES5)会得到同样的效果:
array.some(function(item) {
return notValid(item);
});
#2
18
You could use variant of jQuery is
function which accepts a predicate:
您可以使用jQuery is函数的变体,它接受谓词:
$(array).is(function(index) {
return notValid(this);
});
#3
4
You should use an ordinary for
loop (not for ... in
), which will only loop through array elements.
你应该使用一个普通的for循环(不是for…)在)中,它只会循环遍历数组元素。
#4
4
Xion's answer is correct. To expand upon his answer:
Xion的回答是正确的。进一步阐述他的回答:
jQuery's .is(function)
has the same behavior as .NET's IEnumerable.Any(Predicate<T>)
.
jQuery的.is(函数)与。net的IEnumerable.Any(谓词
From http://docs.jquery.com/is:
从http://docs.jquery.com/is:
Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
如果选择中至少有一个元素符合给定的表达式,则根据表达式检查当前选择并返回true。
#5
1
I suggest you to use the $.grep()
method. It's very close to IEnumerable.Any(Predicate<T>)
:
我建议您使用$.grep()方法。它非常接近于IEnumerable.Any(谓词
$.grep(array, function(n, i) {
return (n == 5);
});
Here a working sample to you: http://jsfiddle.net/ErickPetru/BYjcu/.
这里有一个工作示例:http://jsfiddle.net/ErickPetru/BYjcu/。
#6
1
I would suggest that you try the JavaScript for in
loop. However, be aware that the syntax is quite different than what you get with a .net IEnumerable
. Here is a small illustrative code sample.
我建议您尝试用JavaScript进行循环。但是,请注意,语法与.net IEnumerable是完全不同的。这是一个小型的说明性代码示例。
var names = ['Alice','Bob','Charlie','David'];
for (x in names)
{
var name = names[x];
alert('Hello, ' + name);
}
var cards = { HoleCard: 'Ace of Spades', VisibleCard='Five of Hearts' };
for (x in cards)
{
var position = x;
var card = card[x];
alert('I have a card: ' + position + ': ' + card);
}
#7
0
You might use array.filter (IE 9+ see link below for more detail)
您可以使用数组。过滤器(ie9 +参见下面的链接以获得更详细的信息)
[].filter(function(){ return true|false ;}).length > 0;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter