4

我有以下对象

{
"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.

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

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"];

有没有办法解析/使用这些数组,以便每个从位置对象返回正确的标题值?

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

菜鸟在这里,所以请不要介意这个问题听起来很愚蠢/或没有意义。

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

4

3 回答 3

2
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 是键数组。

于 2013-04-02T10:27:27.487 回答
0

我想你需要这样的东西:

  • 遍历每个数组
  • 遍历数组的值
  • 为每个值嵌套一层更深的对象
  • 将最终值存储在数组中

试试这个:

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"]

我添加了额外的日志记录,因此很明显如果键出现问题。

于 2013-04-02T09:22:57.173 回答
-1

如果数组是所需标题的“路径”,您可以这样做。

请注意,在这种情况下需要“位置”,因为它是路径的开始,并且不需要标题,因为我们一直在寻找它。

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/

于 2013-04-02T09:48:33.917 回答