当引用在数组中时,如何从层次对象中获取值

How do I fetch values from a hierarchical object when the references are in an array?

本文关键字:层次 对象 获取 引用 数组      更新时间:2023-09-26

我有以下对象

{
"locations": {
    "Base 1": {
        "title": "This is base 1",
        "Suburb 1": {
            "title": "Suburb 1 in Base 1",
            "Area A": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            },
            "Another Area": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            }
        },
        "Another Suburb": {
            "title": "Suburb 1 in Base 1",
            "Area A": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            },
            "Another Area": {
                "title": "Title for Area A",
                "Street S1": {
                    "title": "Street S1 title"
                },
                "Street C4": {
                    "title": "Street C4 title"
                },
                "Street B7": {
                    "title": "Street B7 title"
                }
            }
        }
    },
    "Base2": {}
}
}

我得到了从"locations"对象获取"title"的数组,每个数组都可以不同。我知道我可以访问这样的个人价值观:

locations["Base 1"]["title"]
locations["Base 1"]["Another Suburb"]["title"]
locations["Base 1"]["Another Suburb"]["Area A"]["title"]
etc etc.

但如果给我这样的数组,我不确定如何获得标题的值:

AnArray = ["Base 1", "title"];
AnArray = ["Base 1", "Another Suburb", "title"];
AnArray = ["Base 1", "Another Suburb", "Area A", "title"];
AnArray = ["Base 1", "Another Suburb", "Another Area", "title"];

有没有一种方法可以解析/使用这些数组,使每个数组都从locations对象返回正确的标题值?

我必须在每种情况下获取标题的值,我甚至不知道从哪里开始。我尝试加入数组,然后获取"title"值,但这似乎不起作用。

这里没有,所以请不要介意这个问题听起来是否愚蠢/或毫无意义。

所以问题是,当引用在数组中时,我如何从层次对象中获取值?

function getNested(obj, ar_keys) {
    var innerObj = obj;
    for(var i=0,il=ar_keys.length; i<il; i++){
      innerObj = innerObj[ar_keys[i]];
    }
    return innerObj;
}

你可以称之为

 getNested(x['locations'], ar_keys);

其中x是对象,ar_keys是键数组。

我想你需要这样的东西:

  • 遍历每个数组
  • 遍历数组的值
  • 对于每个值,在对象中嵌套一个级别
  • 将最终值存储在数组中

试试这个:

var arrays = [
    ["Base 1", "title"],
    ["Base 1", "Another Suburb", "title"],
    ["Base 1", "Another Suburb", "Area A", "title"],
    ["Base 1", "Another Suburb", "Another Area", "title"]
],
array, i, j,
obj = {
    "locations": {
        "Base1": {
            "title": "Thisisbase1",
            "Suburb1": {
                "title": "Suburb1inBase1",
                "AreaA": {
                    "title": "TitleforAreaA",
                    "StreetS1": {
                        "title": "StreetS1title"
                    },
                    "StreetC4": {
                        "title": "StreetC4title"
                    },
                    "StreetB7": {
                        "title": "StreetB7title"
                    }
                },
                "AnotherArea": {
                    "title": "TitleforAreaA",
                    "StreetS1": {
                        "title": "StreetS1title"
                    },
                    "StreetC4": {
                        "title": "StreetC4title"
                    },
                    "StreetB7": {
                        "title": "StreetB7title"
                    }
                }
            },
            "AnotherSuburb": {
                "title": "Suburb1inBase1",
                "AreaA": {
                    "title": "TitleforAreaA",
                    "StreetS1": {
                        "title": "StreetS1title"
                    },
                    "StreetC4": {
                        "title": "StreetC4title"
                    },
                    "StreetB7": {
                        "title": "StreetB7title"
                    }
                },
                "AnotherArea": {
                    "title": "TitleforAreaA",
                    "StreetS1": {
                        "title": "StreetS1title"
                    },
                    "StreetC4": {
                        "title": "StreetC4title"
                    },
                    "StreetB7": {
                        "title": "StreetB7title"
                    }
                }
            }
        },
        "Base2": {}
    }
},
current, key,
result = [];
for (i = 0; i < arrays.length; i++) {
    array = arrays[i];
    // reset current to the locations object
    current = obj.locations;
    for (j = 0; j < array.length; j++) {
        // remove spaces from the keys
        key = array[j].replace(' ', '', 'gi');
        if (current[key]) {
            // if there is such key then nest further
            current = current[key];
        } else {
            // there is no such key - log it
            console.log(key);
        }
    }
    if (typeof current === "string") {
        // if you have reached a string with the nesting add it to the result
        result.push(current);
    }
}
// output the final result
console.log(result);

在我的情况下,它输出:

["Thisisbase1", "Suburb1inBase1", "TitleforAreaA", "TitleforAreaA"]

我添加了额外的日志记录,这样就可以清楚地知道密钥是否出了问题。

如果数组是所需标题的"路径",则可以这样做。

请注意,在这种情况下,"位置"是必需的,因为它是路径的起点,而不需要标题,因为我们一直在寻找它。

var path = ["locations", "Base1", "AnotherSuburb"];
(function search(o, a){
    for(var p in o){
        if(typeof o[p] === 'object' && path.indexOf(p) !== -1){
            a.push(p);
            if(a.join('') === path.join('')){
                // match, print the title
                console.log(o[p].title);
            }
            else{
               search(o[p], a);
            }
        }
    }
})(object, []);

将打印Suburb1inBase1

演示:http://jsfiddle.net/louisbros/Fq63D/