Javascript 当引用位于数组中时,如何从分层对象获取值?
我有以下目标Javascript 当引用位于数组中时,如何从分层对象获取值?,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我有以下目标 { "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": {
{
"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["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"];
但是,如果给我这样的数组,我不确定如何获得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”值,但这似乎不起作用
这里没有问题,所以请不要介意这个问题听起来是否愚蠢/或毫无意义
所以问题是,当引用位于数组中时,如何从分层对象中获取值?我想您需要这样的东西:
- 遍历每个数组
- 遍历数组的值
- 对于每个值,在对象中嵌套一层
- 将最终值存储在数组中
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);
我添加了额外的日志记录,这样就可以清楚地知道键是否出错。如果数组是指向所需标题的“路径”,您可以这样做 请注意,在这种情况下需要“位置”,因为它是路径的起点,而不需要标题,因为我们一直在寻找它
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, []);
将在BASE1中打印郊区1
演示:
其中x是您的对象,ar_keys是一组键。Wow这是问过并回答过的问题了吗,例如,和。您显示的不是有效的对象或数组定义,因此很难为您提供帮助,因为如何访问对象的细节在很大程度上取决于对象的实际结构。如果内存中有此项,您应该能够使用浏览器中内置的开发工具以有效格式显示它,然后将其复制并粘贴到此处。这些是什么类型的数组?我不明白你是如何或为什么以这种方式获得顶级位置阵列的。。。所以你说你得到了4个数组对象而不是一个?谢谢你的参考T.J。。。我刚刚用一个有效的JSON对象更新了这个问题。@Azurlake-我有数组来查询每种情况下的标题(对于每个数组),查询应该从locations对象返回正确的标题。这个问题中的数组只是一个例子。很好的递归,但是你可以遍历对象中的每个属性。您可以使用
hasOwnProperty
方法,也可以根本不迭代对象属性。OP在数组值中也有空格。。。我更新了问题来解决这个问题。感谢您提供的解决方案:)
getNested(x['locations'], ar_keys);