0

I have the following array

{
    "id": "parent",
    "code": "parent",
    "children": [
        {
            "id": "rtsp",
            "code": "rtsp",
            "children": [
                {
                    "id": "001",
                    "code": "cam30",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "002",
                    "code": "cam31",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "003",
                    "code": "cam32",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "004",
                    "code": "cam10",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "005",
                    "code": "cam11",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "006",
                    "code": "cam12",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "007",
                    "code": "cam13",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "008",
                    "code": "cam14",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "009",
                    "code": "cam15",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "010",
                    "code": "cam16",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "011",
                    "code": "cam17",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "012",
                    "code": "cam18",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "013",
                    "code": "cam19",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "014",
                    "code": "cam9",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "015",
                    "code": "cam7",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "016",
                    "code": "cam8",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "017",
                    "code": "cam5",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                },
                {
                    "id": "018",
                    "code": "cam6",
                    "source": "rtsp://192.168.43.29:8554/test",
                    "sourceFullScreen": "rtsp://192.168.43.29:8554/test"
                }
            ]
        },
        {
            "id": "test",
            "code": "test",
            "children": [
                {
                    "id": "cam100",
                    "code": "cam100",
                    "source": "cam100",
                    "sourceFullScreen": "cam100"
                },
                {
                    "id": "zone-a",
                    "code": "zone-a",
                    "children": [
                        {
                            "id": "cam100a",
                            "code": "cam100a",
                            "source": "cam100a",
                            "sourceFullScreen": "cam100a"
                        }
                    ]
                },
                {
                    "id": "cam101",
                    "code": "cam101",
                    "source": "changed",
                    "sourceFullScreen": "changed"
                },
                {
                    "id": "cam102",
                    "code": "cam102",
                    "source": "cam102",
                    "sourceFullScreen": "cam102"
                },
                {
                    "id": "cam103",
                    "code": "cam103",
                    "source": "cam102",
                    "sourceFullScreen": "cam102"
                },
                {
                    "id": "cam105",
                    "code": "cam105",
                    "source": "cam105",
                    "sourceFullScreen": "cam105"
                }
            ]
        },
        {
            "id": "entrepot",
            "code": "entrepot",
            "children": [
                {
                    "id": "cam1100",
                    "code": "cam1100",
                    "source": "rtsp://125.122.15.12",
                    "sourceFullScreen": "rtsp://125.22.122.11"
                },
                {
                    "id": "safe zone",
                    "code": "safe zone",
                    "children": []
                }
            ]
        },
        {
            "id": "zsd",
            "code": "sdf",
            "children": []
        },
        {
            "id": "za",
            "code": "aze",
            "children": []
        },
        {
            "id": "qsd",
            "code": "azeqsd",
            "children": []
        }
    ]
}

I want to write a function where given a code value it gives me back the full path. is there any arrays package with mæde function for this purpose I tried the npm array-tools but no success a.where(json, { "id": idValue }) is giving an empty array, that I was hoping to show recursively.

what I need looks like

searchPath(array,"003") ==> "003,rtsp,parent"

2
  • 2
    That's not an array Commented Apr 19, 2020 at 21:51
  • I do not think that it exists, the structure of objects are by nature very varied. they must be written case by case Commented Apr 19, 2020 at 21:56

3 Answers 3

1

Very similar task. You just should to save all your path and return this path when you find value (key)

Sign up to request clarification or add additional context in comments.

Comments

1

1.flatten object 2. get keys 3. find key

const flatten = (obj, prefix = '', res = {}) => 
  Object.entries(obj).reduce((r, [key, val]) => {
    const k = `${prefix}${key}`
    if(typeof val === 'object'){ 
      flatten(val, `${k}.`, r)
    } else {
      res[k] = val
    }
    return r
  }, res)

const flattendObj  = flatten(obj);
const found_path  = Object.keys(falttendObj).find(key => key === match)

Comments

0

thank you for your answers it helped me learning something, however here is my solution that fits perfectly my needs

function getCamPath(idValue) {
    console.log("++++++++++++++++" + idValue);
    found_path = "";
    var path = "zone -";
    function search(path, obj, target) {
        var found = false;
        for (var k in obj) {
            if (obj.hasOwnProperty(k))
                if (obj[k] === target){
                    value = obj.code 
                    if(value)
                    return path + "-" + value+ "-"
                    else return path;
                }
                 else if (typeof obj[k] === "object") {
                    value = obj.code
                    var result 
                    if(value)
                    result = search(path + "-" + obj.code + "-", obj[k], target);
                    else result = search(path , obj[k], target);

                     if (result)
                         return result;
                    }
        }
        return false;
    }
    var path = search(path,json,idValue);


    console.log("++++++++++++++++" + path);

    return path;

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.