I have a JSON data structure resembling a map.
var map = {
level1 : {
x : {name:'level1 x' , },
y : {name:'level1 y'}
},
level2 : {
x : {name:'level2 x'},
y : {name:'level2 y'}
}
}
To traverse this data, I am provided with a string representing the path to follow:
"level1 x name" , "level2 y name";
How can I access and manipulate the JSON data based on this string path?
This is what I have attempted so far:
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);
However, the map data does not seem to be changing. How can I iterate through the data and update its values accordingly?