根据JavaScript中的属性值从对象数组中选择[duplicate]

时间:2022-08-26 14:06:53

This question already has an answer here:

这个问题已经有了答案:

I have JSON objects that have several properties such as an id and name. I store them in a JavaScript array and then based on a dropdownlist I want to retrieve the object from the JavaScript array based on its id.

我有一些JSON对象,它们具有一些属性,比如id和名称。我将它们存储在一个JavaScript数组中,然后基于下拉列表,根据对象的id从JavaScript数组中检索对象。

Suppose an object has id and name, how do I select them from my array variable?

假设一个对象有id和名称,如何从数组变量中选择它们?

var ObjectsList = data;
var id = $("#DropDownList > option:selected").attr("value");
ObjectsList["id=" + id];

1 个解决方案

#1


58  

Since you already have jQuery, you could use $.grep:

既然已经有了jQuery,可以使用$.grep:

Finds the elements of an array which satisfy a filter function. The original array is not affected.

查找满足筛选函数的数组元素。原始数组不受影响。

So something like this:

所以这样的:

var matches = $.grep(ObjectsList, function(e) { return e.id == id });

that will leave you with an array of matching entries from ObjectsList in the array matches. The above assumes that ObjectsList has a structure like this:

这将为您在数组匹配中保留来自ObjectsList的匹配项数组。上面假设ObjectsList有这样的结构:

[
    { id: ... },
    { id: ... },
    ...
]

If you know that there is only one match or if you only want the first then you could do it this way:

如果你知道只有一个匹配,或者你只想要第一个,你可以这样做:

for(var i = 0, m = null; i < ObjectsList.length; ++i) {
    if(ObjectsList[i].id != wanted_id)
        continue;
    m = a[i];
    break;
}
// m is now either null or the one you want

There are a lot of variations on the for loop approach and a lot of people will wag a finger at me because they think continue is a bad word; if you don't like continue then you could do it this way:

for循环方法有很多变体很多人会对我摇手指因为他们认为continue是一个坏词;如果你不喜欢继续,你可以这样做:

for(var i = 0, m = null; i < ObjectsList.length; ++i) {
    if(ObjectsList[i].id == wanted_id) {
        m = ObjectsList[i];
        break;
    }
}

#1


58  

Since you already have jQuery, you could use $.grep:

既然已经有了jQuery,可以使用$.grep:

Finds the elements of an array which satisfy a filter function. The original array is not affected.

查找满足筛选函数的数组元素。原始数组不受影响。

So something like this:

所以这样的:

var matches = $.grep(ObjectsList, function(e) { return e.id == id });

that will leave you with an array of matching entries from ObjectsList in the array matches. The above assumes that ObjectsList has a structure like this:

这将为您在数组匹配中保留来自ObjectsList的匹配项数组。上面假设ObjectsList有这样的结构:

[
    { id: ... },
    { id: ... },
    ...
]

If you know that there is only one match or if you only want the first then you could do it this way:

如果你知道只有一个匹配,或者你只想要第一个,你可以这样做:

for(var i = 0, m = null; i < ObjectsList.length; ++i) {
    if(ObjectsList[i].id != wanted_id)
        continue;
    m = a[i];
    break;
}
// m is now either null or the one you want

There are a lot of variations on the for loop approach and a lot of people will wag a finger at me because they think continue is a bad word; if you don't like continue then you could do it this way:

for循环方法有很多变体很多人会对我摇手指因为他们认为continue是一个坏词;如果你不喜欢继续,你可以这样做:

for(var i = 0, m = null; i < ObjectsList.length; ++i) {
    if(ObjectsList[i].id == wanted_id) {
        m = ObjectsList[i];
        break;
    }
}