JSON:
var promiseObj = {
"physical": [],
"virtual": []
}
var config = {
"Environments": [
"LH5",
"LH8",
"AMS"
],
"Clusters": {
"LH5": 4,
"LH8": 4,
"AMS": 4
}
};
So I am trying to set different promise objects based on the environments and the clusters in the given JSON object above.
for (var i = 0; i < config.Environments.length; i++) {
promiseObj.physical[config.Environments[i]][config.Clusters[config.Environments[i]]] = $http.get('URL').success(function(data) {
//Successful stuff here
});
}
However when performing this for loop I get the following error:
promiseObj.physical[config.Environments[i]] is undefined
Could someone shed some light into why this is returning undefined, when the object is clearly defined at the start of the document?
promiseObj.physical = [];thereforepromiseObj.physical[anything]will be undefined. It doesn't matter whatconfig.Environments[i]returns because thephysicalarray is empty.config.Environments[i]is astring,promiseObj.physical[string]has wrong syntax (array index cannot be astringvar envname = config.Environments[i];instead of repeating that expression. It makes it easier to read. Then double-check that you really want the multiple levels of nesting in the data structure that the code expects (but currently fails because you can't indexundefined).