我有一个类似 JSON 数据的地图
var map = {
level1 : {
x : {name:'level1 x' , },
y : {name:'level1 y'}
},
level2 : {
x : {name:'level2 x'},
y : {name:'level2 y'}
}
}
我需要遍历这些数据,我将遍历路径作为字符串
"level1 x name" , "level2 y name";
我如何解析来自该字符串路径的 JSON 数据..??
我尝试的是,
var path = "level1 x name".split(" ");
var pointer = map; // assuming it will take reference of map and change will cause to map also
for (var i = 0, len = path.length; i < len; i++) {
if(pointer){
pointer = pointer[path[i]];
}else{
pointer = map[path[i]];
}
}
pointer = "level1 xx";
console.log(map);
但是地图数据没有改变..如何通过引用循环并更改值..?