我在尝试查看包含一个对象的数组中的值是否匹配时遇到问题。
这就是我正在做的事情:
var found = $.inArray(opt.ifpo_width, selectedOptions) > -1;
可以说opt.ifpo.width
包含650
。 selectedOptions
包含一个值为 650
的对象所以我想要 found
返回 0,因为这意味着已找到该值。
这是 console.log
的示例的selectedOptions
:
[Object, Object]
0: Object
active: true
......
ifpo_width: "650" <-- value I am checking
ifpo_x: "153"
ifpo_y: "86"
shown: false
__proto__: Object
1: Object
active: true
ifpo_width: "650" <-- this other object should not be here because there is already a width of the same value.
ifpo_x: "140"
ifpo_y: "102"
.....
您对我如何检查此内容有什么建议和想法selectedOptions
对于使用 opt.ifpo_width
检查的值?
最佳答案
如果 ifpo_width 可以改变,你可以使用这样的函数
function search(property, arr, value) {
var t;
for (t = 0; t < arr.length; t++) {
if (arr[t][property] == value)
return true;
}
return false;
}
并调用它
search("ifpo_width", YourArray, selectedOptions)
否则更简单
function search(arr, value) {
var t;
for (t = 0; t < arr.length; t++) {
if (arr[t].ifpo_width == value)
return true;
}
return false;
}
关于javascript - 在对象中查找值 - JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23334202/