【问题标题】:jQuery specify criteria for searching value in arrayjQuery指定在数组中搜索值的条件
【发布时间】:2012-04-30 17:18:28
【问题描述】:

在 javascript 中,我需要知道数组是否包含值。值是对象,我可以拥有同一对象的不同实例,这意味着 $.inArray(...) 将不起作用。我知道如何使用 $.each(...) 来完成我的任务,我的问题是 - 是否可以将具有值比较逻辑的函数传递给任何 jQuery 方法(参见带有所需 sintax 的示例)?

    // values
var val1 = { id: 1, description: 'First value'};
var val2 = { id: 2, description: 'Second value'};
var val3 = { id: 3, description: 'Third value'};        
// array of values
var values = [ val1, val2, val3 ];
// inArray of jQuery to know if value is in array -> returns TRUE
var isInArray = $.inArray(val2, values) > -1;

// another instance of same object "val2"
var val2_anotherInstance = { id: 2, description: 'Second value'};
// inArray works as expected -> returns FALSE but desirable result is TRUE
var isInArray_anotherInstance = $.inArray(val2_anotherInstance, values) > -1;

// define function for comparing values (any logic can be implemented, for example searching by Id)
var valueComparer = function(first, second) {
    return first.id == second.id && first.description == second.description;
}
// function compares objects and returns true for different instances
alert(valueComparer(val2, val2_anotherInstance));

// desirable sintax:
// is something like this possible ???      
// NOTE next line not correct
isInArray_anotherInstance = $.inArray(val2_anotherInstance, values, valueComparer) > -1;
// actually what I want is specify criteria for searching value in array

【问题讨论】:

  • 不幸的是inArray() 没有功能。你可以使用grep()

标签: javascript jquery arrays contains


【解决方案1】:

试试grep:

var val1 = { id: 1, description: 'First value'};
var val2 = { id: 2, description: 'Second value'};
var val3 = { id: 3, description: 'Third value'};        

var values = [ val1, val2, val3 ];

// another instance of same object "val2"
var val2_anotherInstance = { id: 2, description: 'Second value'};


var items = $.grep(values, function(x) {
    return x.id == val2_anotherInstance.id
})

var found = items.length > 0

为了更优雅,您可以使用布尔聚合函数,如this answer 中提供的那样:

val2_in_array = $.some(values, function() {
    return this.id == val2_anotherInstance.id
});

【讨论】:

    【解决方案2】:

    您可以将此功能用于您的任务:

    $.fn.inArrayCallback = function(needle, haystack, f) {
      var e = -1;
      $.each(haystack,function(index, value){
          if (f(needle,value)) { e = index; return false; }
        });
      return e;
    }
    
    ans = $.fn.inArrayCallback(val2_anotherInstance, values, valueComparer) > -1; 
    // returns true
    

    使用 grep 函数回答将搜索数组中的所有元素,即使已经找到匹配的元素。此功能停止搜索匹配。这在非常大的阵列上可能很重要。

    【讨论】:

    • re:your edit - 这就是为什么我还建议some(),一旦回调返回true,它就会停止。
    • 谢谢!!这正是我要找的
    【解决方案3】:

    您可以使用greep() 函数来过滤您的数组,然后计算生成数组中的项目。但它会处理所有数组,如果你有很多数据,它对性能不友好。

    【讨论】:

      【解决方案4】:

      jquery 地图功能应该可以解决您的问题。 您可以在 map 的回调中实现您的比较逻辑。 refer jQuery map

      【讨论】:

        猜你喜欢
        • 2012-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-20
        • 1970-01-01
        • 2012-07-02
        • 1970-01-01
        相关资源
        最近更新 更多