I'm having trouble creating a JSON object that should look like this:
{
"sreddy-vm-1":["MyDatabase-1"],
"sreddy-vm-2":["MyDatabase-2"],
"sreddy-vm-3":["MyDatabase-3", "MyDatabase-4", "MyDatabase-5"]
}
However, the output I'm getting is different.
{"sreddy-vm-1":["MyDatabase-1"],"sreddy-vm-2":["MyDatabase-2"], "sreddy-vm-3":3}
Everything works fine until additional elements are added to the array for sreddy-vm-3
, at which point it assigns 2 and then 3. I can't seem to figure out what's causing this issue. As someone new to JavaScript, any help or insight would be greatly appreciated. Thank you!
Code
// snippet of 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));