I'm trying to grammatically follow a path inside a JSON object, and replace the object with a blank one.
I'm using this method to reach the object desired, and I can verfiy that the object is reached.
$scope.path = "countries.canada.territories.yukon";
$scope.remove = function () {
var pathInArrayForm = $scope.path.split('.');
currentObject = $scope.countries;
for (var i = 0; i < pathInArrayForm.length; i++){
currentObject = currentObject[pathInArrayForm[i]];
}
currentObject.capital = "Montreal";
}
However, when I try to replace the object with a blank one, via
currentObject = {};
Nothing happens. It doesn't change anything! When I use:
currentObject.capital = "Montreal"
it works, but if I just use the object plain, nothing happens.
Please see my JSFiddle: http://jsfiddle.net/ay1wpr5L/2/
My question is, how can I replace an object nested inside other JSON objects, with a blank {}?