简体   繁体   English

在对象的JavaScript数组中查找属性

[英]Finding a property within a JavaScript array of object

I have a JavaScript array which looks like this. 我有一个看起来像这样的JavaScript数组。 The array contain object of different type. 该数组包含不同类型的对象。

[
    {
        "className": "XXXX",
        "targets": [
            0,
            1,
            2,
            3,
            4
        ],
        "sClass": "lg" 
    },
    {
        "iDataSort": 0
    },
    {
        "targets": [
            9,
            10
        ]
    },
    {
        "targets": [
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15
        ],
        "visible": false,
        "bVisible": false
    }
]

I want to search an element (object) in this array which contains two property with name targets and visible and once I find this object, I want to update the value of targets property. 我想在此数组中搜索一个元素(对象),其中包含两个具有名称targetsvisible属性的属性,一旦找到该对象,就想更新targets属性的值。

What is the most efficient way to do this? 最有效的方法是什么? Is it possible to use underscorejs or lodash for this? 可以使用underscorejs或lodash吗?

Try to use $.each() along with .hasOwnProperty(propertyName) at this context, 尝试在这种情况下将$.each().hasOwnProperty(propertyName)一起使用,

$.each(test, function (i, val) {
    if (val.hasOwnProperty("visible") && val.hasOwnProperty("targets")) {
        alert('yes..')
    }
});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM