简体   繁体   English

收集JSON object中的所有路径

[英]Collect all paths in JSON object

I'm dealing with the following JavaScript object:我正在处理以下 JavaScript object:

{
    "gender": "man",
    "jobinfo": {
        "type": "teacher"
    },
    "children": [
        {
            "name": "Daniel",
            "age": 12,
            "pets": [
                {
                    "type": "cat", 
                    "name": "Willy",
                    "age": 2
                },
                {
                    "type": "dog", 
                    "name": "Jimmie",
                    "age": 5
                }
            ]
        }
    ]
}

I want to print out each of the paths (keys and array indices) within the object, including the parents (ie children should be printed as should everything in it).我想打印出 object 中的每个路径(键和数组索引),包括父母(即应该打印children ,因为应该打印其中的所有内容)。

gender
jobinfo,
jobinfo.type,
children,
children.0.name,
children.0.age,
children.0.pets,
children.0.pets.0.type,
children.0.pets.0.name,
children.0.pets.0.age,
children.0.pets.1.type,
children.0.pets.1.name,
children.0.pets.1.age

I tried this code with modifications but it didnt work for me:我尝试修改此代码,但它对我不起作用:

function getPath(object) {
    for (key in object) {
        if (Array.isArray(object[key]) === true) {
            console.log(key)
            getPath(object[key])
        } else if (typeof object[key] === 'object') {
            console.log(key)
            getPath(object[key])
        } else {
            console.log(key)
        }
    }
}

It's printing all keys in the JSON, but I'm struggling with joining the paths, especially in nested elements.它正在打印 JSON 中的所有键,但我正在努力加入路径,尤其是在嵌套元素中。

This works:这有效:

 const data = {"gender":"man","jobinfo":{"type":"teacher"},"children":[{"name":"Daniel","age":12,"pets":[{"type":"cat","name":"Willy","age":2},{"type":"dog","name":"Jimmie","age":5}]}]}; const getPath = (currPath, item) => { console.log(currPath); if (Array.isArray(item)) { item.forEach((el, idx) => getPath(`${currPath}.${idx}`, el)); } else if (typeof item == "object") { Object.entries(item).forEach(([key, value]) => { getPath(`${currPath}.${key}`, value); }); } }; Object.entries(data).forEach(([key, value]) => { getPath(key, value); });

Basically I just loop through each of the entries in the initial object, using the key as the path at that stage and checking if the value is an object or array.基本上我只是循环遍历初始 object 中的每个条目,在那个阶段使用键作为路径并检查值是 object 还是数组。 I always print the path within the function (to provide the outer layers you want) and then I recurse over the inner layers, adding to the path as needed.我总是在 function 中打印路径(以提供您想要的外层),然后递归遍历内层,根据需要添加到路径中。

In this version array keys that are consisted of numbers like 'children.0' and so on handled and this gives the result exactly what you wanted:在此版本中,由数字组成的数组键,如 'children.0' 等被处理,这给出了你想要的结果:

 const json = {"gender":"man","jobinfo":{"type":"teacher"},"children":[{"name":"Daniel","age":12,"pets":[{"type":"cat","name":"Willy","age":2},{"type":"dog","name":"Jimmie","age":5}]}]}; function getPath(object, previousPath) { for (key in object) { let currentPath = previousPath? `${previousPath}.${key}`: key if (Array.isArray(object[key])) { console.log(currentPath) getPath(object[key], currentPath) } else if (typeof object[key] === 'object') { if (.Array.isArray(object)) { // skipping logging array keys like children.0 console,log(currentPath) } getPath(object[key]. currentPath) } else { console.log(currentPath) } } } getPath(json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM