【问题标题】:Get the location of an object in an array with JS/jQuery使用 JS/jQuery 获取数组中对象的位置
【发布时间】:2015-01-30 21:21:06
【问题描述】:

假设我有一个如下数组:

[
    {
        "name": "list",
        "text": "SomeText1"
    },
    {
        "name": "complex",
        "text": "SomeText2",
        "config": {
            "name": "configItem",
            "text": "SomeText3",
            "anotherObject": {
                "name": "anotherObject1",
                "text": "SomeText4"
            }
        }
    }
]

我正在使用这个很棒的代码来获取具有特定键 (http://techslides.com/how-to-parse-and-search-json-in-javascript) 的所有对象。在我的示例中,它是 getObjects(data,'text',''),由于文本作为键的出现,它将所有节点作为对象返回。

我唯一的问题是,我需要知道返回的对象在整个数组中的位置。

有什么方法可以得到吗?或者至少是对象的深度结合数组?

getObjects(r,'text','')[0](名称 = 列表)-> 深度 1

getObjects(r,'text','')[1](名称 = 复杂)-> 深度 1

getObjects(r,'text','')[2] (name = configItem) -> 深度 2

【问题讨论】:

标签: javascript jquery


【解决方案1】:

你将需要这些方面的东西:

function getObjectsDepth(obj, key, val, depth) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjectsDepth(obj[i], key, val,++depth));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push(depth);
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push(depth);
            }
        }
    }
    return objects;
}

这将返回对象的深度,但不返回对象本身,只需根据您想要的计数方式传递 0 或 1 作为最后一个参数。 如果你同时想要物体和深度,你需要obj.push({'obj':obj,'depth':depth})

【讨论】:

  • 谢谢!我遇到了另一个问题。如果我调用 getObjects(r,'config','') 我的返回值为 null,尽管我的数组中有一个对象。
  • 这是因为 'config' 是一个对象,所以它被跳过了。你应该在if (typeof obj[i] == 'object') { 之后添加objects.push(obj); 才能达到这个目的。
  • 这会给我很多返回值,我在查询 Key = text 时不需要这些返回值,我认为我需要调试它 - 提前致谢。
  • 其实有更优雅的解决问题的办法,直接删掉else这个词
【解决方案2】:

通过这个改变getObjects函数:

function getObjects(obj, key, val, depth) {
    var objects = [];
    depth = typeof depth !== 'undefined' ? depth : 0;
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            depth ++;
            objects = objects.concat(getObjects(obj[i], key, val, depth));    
        } else 
        //if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
        if (i == key && obj[i] == val || i == key && val == '') { //
            objects.push({"obj":obj,"depth": depth});
        } else if (obj[i] == val && key == ''){
            //only add if the object is not already in the array
            if (objects.lastIndexOf(obj) == -1){
                objects.push({"obj":obj,"depth": depth});
            }
        }
    }
    return objects;
} 

现在你得到了深度和对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多