首页 > 解决方案 > 在 Javascript 对象中搜索特定 ID 的位置?

问题描述

我有一个包含许多不同部分的 Javascript 对象。如何搜索所有部分以找到特定 ID 的位置?我正在搜索的 ID 不在特定位置,并且可以位于任何树枝中。

例如,我正在搜索此 ID:

xobmnbjxg0g_1527269346261

我正在尝试输出该 ID 的位置,即:

app['structure'][0]['if-children'][0]['id']

我的 Javascript 对象:

var app = {
    "structure": [
        {
            "id": "0",
            "type":"IF",
            "parameters": [
                {
                    "id": "xobmnbjxg0g_1527269346260",
                    "type": "field",
                    "value": "CV_TEST_SPOT1X"
                },
                {
                    "id": "2",
                    "type": "operator",
                    "value": "="
                },
                {
                    "id": "3",
                    "type": "field",
                    "value": "North America"
                }
            ],
            "if-children": [
                {
                    "id": "xobmnbjxg0g_1527269346261",
                    "type":"IF",
                    "parameters": [
                        {
                            "id": "1",
                            "type": "field",
                            "value": "CV_TEST_SPOT1"
                        },
                        {
                            "id": "2",
                            "type": "operator",
                            "value": "="
                        },
                        {
                            "id": "3",
                            "type": "field",
                            "value": "North America"
                        }
                    ],
                    "if-children":[


                    ],
                    "else-children":[


                    ]
                }
            ],
            "else-children":[
                {
                    "id": "xobmnbjxg0g_1527269346262",
                    "type":"IF",
                    "parameters": [
                        {
                            "id": "1",
                            "type": "field",
                            "value": "CV_TEST_SPOT1"
                        },
                        {
                            "id": "2",
                            "type": "operator",
                            "value": "="
                        },
                        {
                            "id": "3",
                            "type": "field",
                            "value": "North America"
                        }
                    ],
                    "if-children":[
                        {
                            "id":"xobmnbjxg0g_152726934626X"
                        }   
                    ],
                    "else-children":[
                        {
                            "id":"xobmnbjxg0g_152726934626Y"
                        }   

                    ]
                }
            ]

        },
        {
            "id": "xobmnbjxg0g_1527269346263",
            "type":"IF",
            "parameters": [
                [
                    {
                        "id": "1",
                        "type": "field",
                        "value": "CV_TEST_SPOT1"
                    }
                ]
            ],
            "if-children": [
                {
                    "id": "xobmnbjxg0g_1527269346264",
                    "type":"IF",
                    "parameters": [
                        [
                            {
                                "id": "1",
                                "type": "field",
                                "value": "CV_TEST_SPOT1"
                            }
                        ]
                    ],
                    "if-children":[
                        {
                            "id": "xobmnbjxg0g_1527269346265",
                            "type":"IF",
                            "parameters": [
                                {
                                    "id": "1",
                                    "type": "field",
                                    "value": "CV_TEST_SPOT1"
                                }
                            ],
                            "if-children":[
                                {
                                    "id":"xobmnbjxg0g_1527269346266"
                                }
                            ],
                            "else-children":[
                                {
                                    "id":"xobmnbjxg0g_1527269346267"
                                }                           
                            ]
                        }
                    ],
                    "else-children":[
                        {
                            "id":"xobmnbjxg0g_1527269346268"
                        }                   
                    ]
                }
            ],
            "else-children":[
                {
                    "id":"xobmnbjxg0g_1527269346269"
                }           
            ]

        }
    ]
};

标签: javascript

解决方案


有趣的谜题/问题。

很确定我缺少一些边缘情况,但这似乎通过了一些测试。

function is(obj, type){
    return Object.prototype.toString.call(obj) === `[object ${type}]`;
}

function findPosition(obj, mykey, myval, res){
    if(is(obj, "Object")){
        if(mykey in obj && obj[mykey] === myval){
          res.tree.push(mykey);
          res.found = true;
        } else {
          for( let key in obj){
            if(res.found) break;
            res.tree.push(key);
            findPosition(obj[key], mykey, myval, res);
          }
          if(!res.found) res.tree.pop();
        }
    } else if(is(obj, "Array")){
        for(let i = 0; i < obj.length; i++){
            if(res.found) break;
            res.tree.push(i);
            findPosition(obj[i], mykey, myval, res);
        }
        if(!res.found) res.tree.pop();
    } else {
        res.tree.pop();
    }

    return res;
}

使用和输出

findPosition([{one: { two: [{id: [{id:'my'}]}]}}], "id", "mys", {tree:[], found: false})
> tree: Array(0), found: false}

findPosition([{one: { two: [{id: [{id:'my'}]}]}}], "id", "my", {tree:[], found: false})
> {found: true, tree: [0, "one", "two", 0, "id", 0, "id"]}

要查找您正在迭代的当前 obj 是否为 an,Array您还可以使用Array.isArray


推荐阅读