【问题标题】:What is the equivalent of prototype .any in Jquery?Jquery 中原型 .any 的等价物是什么?
【发布时间】:2013-03-27 10:53:53
【问题描述】:

我们在 Prototype 中有一个名为 .any 的函数。我想要和 Jquery 一样的东西。

我的原型代码是:

 if (item_name == '' || $R(1,ind).any(function(i){return($F("bill_details_"+i+"_narration") == item_name)})) {
     alert("This item already added.");
 }

我想使用 Jquery 执行等效函数。

请帮助我实现所需的输出。在此先感谢..

【问题讨论】:

  • 如果有人投反对票。请评论原因..
  • ind$F 里面是什么?
  • ind 是一个类似于总行数的数字。 1,2,...
  • @jantimon 使用此代码是为了避免重复项。一旦我们不应该允许再次添加,就添加了一项。
  • 所以你根本没有数组?

标签: jquery prototypejs


【解决方案1】:

对于 IE 9+,它是内置的:

some() 方法测试数组中的某个元素是否通过了提供的函数实现的测试。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some

[2, 4, 6, 8, 10].some(function(n) { return n > 5; });
// -> true (the iterator will return true on 6)

对于 IE 8 及以下版本:

原型any

[2, 4, 6, 8, 10].any(function(n) { return n > 5; });
// -> true (the iterator will return true on 6)

你可以使用jQuery.grep:

jQuery.grep([2, 4, 6, 8, 10], function(n) { return n > 5; }).length > 0;
// -> true (as grep returns [6, 8, 10])

下划线_.any_.some

_.any([2, 4, 6, 8, 10], function(n) { return n > 5; });
// -> true (the iterator will return true on 6)

【讨论】:

  • 你可以为我上面的原型代码发布等效的 jquery。请
  • $F 仍在使用 Prototype,而不是 jQuery。
  • 至于性能,请注意 .any 应该停止关注第一次出现,而 .grep 将遍历整个数组。
  • 我想知道你是否可以使用类似 stackoverflow.com/a/22268145/582917 的东西来突破聚合函数的非短路版本。
  • 对于 jquery,$.grep().length 的效率不如 $.is()
【解决方案2】:

ES5 有一个名为 Array.prototype.some 的内置函数,它测试数组中的 any 元素是否与谓词函数匹配,并在找到匹配元素后立即停止迭代。

.some(function(el) {
    return el.value === item_name;
});

然后您的问题就变成了创建所需元素的数组之一,这比在 Prototype 中更难,因为 jQuery 中没有“范围”运算符。幸运的是 $.map 迭代空元素,即使内置的 Array.prototype.map 没有,所以你可以使用 new Array(ind)

var found = $.map(new Array(ind), function(_, x) {
    return "bill_details_" + (x + 1) + "_narration";
}).some(function(id) {
    var el = document.getElementById(id);
    return el && (el.value === item_name);
});

上面的链接包含一个用于旧版浏览器的.some 填充程序。

【讨论】:

    【解决方案3】:

    JQuery 有 .is() 方法,来自文档:Check the current matched set of elements against a selector, element, or jQuery object and return *true* if at least one of these elements matches the given arguments。因此等效代码是:

     if (item_name == '' || $([1,ind]).is(function(i) { return $('#bill_details_'+i+'_narration').attr('name') == item_name; })) {
          alert("This item already added.");
     }
    

    【讨论】:

      【解决方案4】:

      看来http://api.jquery.com/jQuery.grep/ 就是你要找的。​​p>

      试试

      if (item_name == '' || $.grep([1,ind],function(i){return($("#bill_details_"+i+"_narration").attr("name") == item_name)}).length>0) {
           alert("This item already added.");
       }
      

      【讨论】:

        【解决方案5】:

        根据 JQuery 的文档,如果您在回调函数中返回 false,它将打破循环:

        我们可以通过让回调函数返回 false 来在特定的迭代中打破 $.each() 循环。返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。

        取自:http://api.jquery.com/jquery.each/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-19
          • 1970-01-01
          • 2011-03-25
          • 1970-01-01
          • 2014-07-03
          • 2011-07-23
          • 2012-09-22
          • 2023-04-04
          相关资源
          最近更新 更多