当引用在数组中时,如何从层次对象中获取值
How do I fetch values from a hierarchical object when the references are in an array?
我有以下对象
{
"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/
相关文章:
- 从json对象聚集数据并创建层次结构
- 无法通过jQuery查找来定位层次结构中的对象
- 将 FORM 层次结构反序列化为 JS 对象
- 如何为层次结构目的构建 Json 对象
- 按层次结构和名称对具有层次结构的对象数组进行排序
- 如果我忽略对象在层次结构中的深度,如何为对象的字段赋值
- 从HTML大纲创建层次结构对象
- 如何解析一个多层次对象的键中包含点(.)的对象
- Javascript访问相同层次结构下的对象属性
- 创建JSON对象的层次结构数组
- 传递一个对象's的层次结构作为它的参数'的父函数
- 从字符串列表到层次对象
- Javascript:跟踪函数父对象层次结构,以便从窗口上下文调用它
- Javascript对象层次结构简化
- 在Angular2的父/子组件层次结构中,你应该在什么时候订阅一个可观察对象?
- 在angular中观察复杂对象层次的深度和内容
- 对象层次结构中的变量作用域
- 从伪层次结构创建嵌套对象
- 从深层层次结构中检索具有给定属性的对象
- 当引用在数组中时,如何从层次对象中获取值