JavaScript/jQuery 等效于 LINQ Any()

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

JavaScript/jQuery equivalent of LINQ Any()

javascriptjqueryforeachienumerableany

提问by dilbert

Is there an equivalent of IEnumerable.Any(Predicate<T>)in JavaScript or jQuery?

IEnumerable.Any(Predicate<T>)在 JavaScript 或 jQuery 中是否有等价物?

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:

我正在验证项目列表,并希望在检测到错误时尽早中断。我可以使用$.each,但我需要使用外部标志来查看是否确实找到了该项目:

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 forwith JavaScript arrays because it iterates over all of its members, not just values.

什么是更好的方法?我不喜欢for在 JavaScript 数组中使用普通的,因为它遍历它的所有成员,而不仅仅是值。

回答by Sean Vieira

These days you could actually use Array.prototype.some(specced in ES5) to get the same effect:

Array.prototype.some如今,您实际上可以使用(在 ES5 中指定)来获得相同的效果:

array.some(function(item) {
    return notValid(item);
});

回答by Xion

You could use variant of jQuery isfunction which accepts a predicate:

您可以使用is接受谓词的 jQuery函数的变体:

$(array).is(function(index) {
    return notValid(this);
});

回答by SLaks

You should use an ordinary forloop (not for ... in), which will only loop through array elements.

您应该使用普通for循环(不是for ... in),它只会循环遍历数组元素。

回答by Scott Rippey

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(function)行为与 .NET 的IEnumerable.Any(Predicate<T>).

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,如果选择的至少一个元素适合给定的表达式。

回答by Anthony Johnston

You might use array.filter (IE 9+ see link below for more detail)

您可能会使用 array.filter(IE 9+,请参阅下面的链接以获取更多详细信息)

[].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

回答by Vivian River

I would suggest that you try the JavaScript for inloop. However, be aware that the syntax is quite different than what you get with a .net IEnumerable. Here is a small illustrative code sample.

我建议您尝试 JavaScriptfor in循环。但是,请注意语法与您使用 .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);
}

回答by Erick Petrucelli

I suggest you to use the $.grep()method. It's very close to IEnumerable.Any(Predicate<T>):

我建议你使用这个$.grep()方法。它非常接近IEnumerable.Any(Predicate<T>)

$.grep(array, function(n, i) {
  return (n == 5);
});

Here a working sample to you: http://jsfiddle.net/ErickPetru/BYjcu/.

这里有一个工作示例:http: //jsfiddle.net/ErickPetru/BYjcu/

回答by razon

With https://www.npmjs.com/package/manipulathat implements all C# LINQ methods, it would be:

使用实现所有 C# LINQ 方法的https://www.npmjs.com/package/manipula,它将是:

Manipula.from([0,1]).any(x=>x>0)