I have defined a JS array as below:
var forecastJSArray=new Array();
and then I am trying to add my json objects into this array using push, as below:
$.getJSON("api-detail.json")
.then(function (forecast){
$.each(forecast, function() {
self.forecastJSArray.push({
rank: this.rank,
rep: this.rep,
total: this.total,
});
});
})
;
But when I run this code, it gives me an error as: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined
My final array should look like this:
[
{
"rank" : 1,
"rep" : "rep1",
"total" : 10,
}, {
//...etc
}
]
Would be really grateful if someone can give me an idea where I am going wrong with this. Thanks a lot in advance.
EDIT: The code is enclosed inside an oracle-jet view model (which uses knockoutjs view model), and this is something like this, with a RequireJS block:
define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojselectcombobox',
'ojs/ojtable', 'ojs/ojbutton'], function(oj, ko, $) {
function DashboardViewModel() {
var self = this;
... earlier code
... earlier code
}
return new DashboardViewModel();
});
as $.getJSON returns asynchronously, I am finding it difficult to use the data outside the callback function. So trying to push the data into a JS array first. Please let me know if you need any additional information. TIA.
self..