javascript - 在对象中查找值 - JQuery

标签 javascript jquery

我在尝试查看包含一个对象的数组中的值是否匹配时遇到问题。

这就是我正在做的事情:

var found = $.inArray(opt.ifpo_width, selectedOptions) > -1;

可以说opt.ifpo.width包含650selectedOptions包含一个值为 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/

相关文章:

javascript - Google Meet 如何显示 CPU 使用率?

javascript - 查看 Node 请求体

javascript - 流畅的 SVG 动画

javascript - 真的不知道如何为这个 JS 函数创建时间循环

javascript - jquery .animate() 高度为自动

javascript - Heatmaps.js 未捕获的 ReferenceError

javascript - token "."出现语法错误,jshint 中此 token 后应有标识符

javascript - 需要在 JS 和/或中重述左侧?

javascript - 在 iframe 顶部跟踪滑动

javascript - 错误: You called Tooltipster's "show" method on an uninitialized element