首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript数组获取子路径

Javascript数组获取子路径
EN

Stack Overflow用户
提问于 2020-04-19 21:46:11
回答 3查看 238关注 0票数 0

我有以下数组

代码语言:javascript
运行
复制
{
    "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": []
        }
    ]
}

我想要写一个函数,在这个函数中,给定一个代码值,它会给我返回完整的路径。是否有任何带有m de函数的数组包,为此目的,我尝试了npm数组工具,但是没有成功,a.where(json, { "id": idValue })给出了一个空数组,我希望递归地展示它。

我需要的东西看起来像

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-04-19 23:26:52

谢谢你的回答,它帮助我学到了一些东西,然而,这是我的解决方案,完全符合我的需要。

代码语言:javascript
运行
复制
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;

}
票数 0
EN

Stack Overflow用户

发布于 2020-04-19 22:02:17

Very similar task。您应该保存所有路径,并在找到值(键)时返回此路径。

票数 1
EN

Stack Overflow用户

发布于 2020-04-19 22:17:05

1.扁平对象2.获取键3.查找键

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61312040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档