jQuery:如何找到具有某个属性等于某个值的对象?

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

jQuery: How to find an object with a certain property equals a certain value?

jquery

提问by David

Using jQuery, how do I iterate through an array of objects and return the one that meets certain criteria?

使用 jQuery,如何遍历对象数组并返回满足特定条件的对象?

回答by jfriend00

You can use the jQuery grepfunction:

您可以使用jQuery grep函数:

var matches = jQuery.grep(array, function(item) {
    // this is a reference to the element in the array
    // you can do any test on it you want
    // return true if you want it to be in the resulting matches array
    // return false if you don't want it to be in the resulting matches array

    // for example: to find objects with the Amount property set to a certain value
    return(item.Amount === 100);
});
// matches contains all objects that matches
if (matches.length) {
    // first match is in matches[0]
}

If your condition you want to test for is anything other than a strict equality, then you will have to use some sort of array iteration that executes your custom comparison. You could do it with .each()or with .grep()depending upon what type of output you want.

如果要测试的条件不是严格相等,则必须使用某种数组迭代来执行自定义比较。您可以使用.each()或使用,.grep()具体取决于您想要的输出类型。

If you condition was a strict equality, you could use jQuery.inArray().

如果您的条件是严格相等,则可以使用jQuery.inArray().

Obviously, you don't need jQuery for this as you could just iterate through the array yourself in plain javascript and implement whatever test you wanted. One advantage of using plain javascript is that you can break out of the iteration when you've found the result you want.

显然,您不需要 jQuery,因为您可以自己用普通的 javascript 遍历数组并实现您想要的任何测试。使用普通 javascript 的一个优点是,当您找到想要的结果时,您可以中断迭代。

In regular javascript:

在常规javascript中:

for (var i = 0, len = array.length; i < len; i++) {
    if (array[i].Price === 100) {
        // match is in array[i]
        break;
    }
}

回答by labroo

$([1,2,2,4]).filter(function(i,n){return n==2;});

this will return both 2's

这将返回两个 2

basically the array can be an array of dom elements or any array actually, if it is a array returned by a jQuery selector you can do some thing like

基本上,该数组可以是 dom 元素的数组,也可以是实际上的任何数组,如果它是由 jQuery 选择器返回的数组,则可以执行以下操作

$('div.someClass').filter(function(){return $(this).hasClass('someOtherClass')})

just for eg-> this will return all divs which have both someClass and someOtherClass (note: there are other ways to do this)

仅用于 eg-> 这将返回所有具有 someClass 和 someOtherClass 的 div(注意:还有其他方法可以做到这一点)

updating as per your comment, you can do

根据您的评论更新,您可以

$(yourArray).filter(function(i,n){return n.Amount && n.Amount == conditionValue;});

回答by driangle

You don't really need jQuery to do what you need:

你真的不需要jQuery来做你需要的:

var objects = [{id:23, amount:232}, {id:42, amount: 3434}, ...]

// the function which finds the object you want, pass in a condition function
function findObject(objectMeetsCondition){
     for(var i = 0 ; i < objects.length ; i++){
        if(objectMeetsCondition(objects[i])) return objects[i];
     }
}

// your custom condition that determines whether your object matches
function condition(obj){
   return obj.amount == 3434;
}

findObject(condition); // returns {id:42,amount:3434}