0

I have a Javascript object with lots of different sections. How can I search through all of the sections to find the position of a specific ID? The ID's that I am searching for are not in a specific location, and can be located in any of the tree branches.

For example, I am searching for this ID:

xobmnbjxg0g_1527269346261

And I am trying to output the position of that ID, which would be this:

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

My Javascript Object:

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"
                }           
            ]

        }
    ]
};
lol
  • 85
  • 8

1 Answers1

0

Interesting puzzle/question.

pretty sure there are some edge cases im missing but this seems to pass some tests.

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;
}

Usage and output

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"]}

For finding if current obj you are iterating over is an Array you can also use Array.isArray

eltonkamami
  • 5,134
  • 1
  • 22
  • 30
  • Thanks for the response! It is a puzzle, I didnt realize how complex it was until I tired to build it. I tried your code and it gave me 89 variables in the tree array when it should only be 6. Not sure what's wrong. Test with this JS object: https://jsfiddle.net/jtndone7/ – lol May 25 '18 at 20:03
  • @lol which id are you trying to find. this is what i get https://jsfiddle.net/jtndone7/1/ – eltonkamami May 25 '18 at 20:12
  • I tested with `xobmnbjxg0g_1527269346262` – lol May 25 '18 at 20:38
  • @lol see this fiddle https://jsfiddle.net/jtndone7/2/ it gives me a correct result – eltonkamami May 25 '18 at 20:42
  • Seems to be working perfectly now!!!!!!!! THANK YOU SO MUCH!!!!!!!!!!!!!!!!!! – lol May 25 '18 at 21:16