I'm trying to generate a JSON object which I expect to look like this:
{
"sreddy-vm-1":["MyDatabase-1"],
"sreddy-vm-2":["MyDatabase-2"],
"sreddy-vm-3":["MyDatabase-3", "MyDatabase-4", "MyDatabase-5"]
}
but its generating this instead
{"sreddy-vm-1":["MyDatabase-1"],"sreddy-vm-2":["MyDatabase-2"], "sreddy-vm-3":3}
The correct json is generated until the point at which each key has one element in the array, the moment second element is added to the array for sreddy-vm-3 it assigns 2 and then in the next(last) loop it ends up assigning 3.
I can't figure out whats going on. I'm new to Javascript in general.Please help. Thank you!
Code
// part of the code
$scope.forests = ["MyDatabase-1", "MyDatabase-2", "MyDatabase-3", "MyDatabase-4", "MyDatabase-5"];
$scope.forestsOnHosts = {};
angular.forEach($scope.forests, function(forest, key){
$scope.forestsOnHosts[host] = function () {
if ($scope.forestsOnHosts[host] === undefined || $scope.forestsOnHosts[host] === null) {
console.log('******************');
return new Array(forest);
} else {
console.log(JSON.stringify($scope.forestsOnHosts[host]));
var arr = new Array($scope.forestsOnHosts[host]);
return arr.push(forest);
}
}.call();
}
console.log(JSON.stringify($scope.forestsOnHosts));