0

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));
1
  • what is host in your code? Commented May 7, 2014 at 8:32

1 Answer 1

2
return arr.push(forest);

this does not return an array, as you might expect.

arr.push(forest);
return arr;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.