Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  124] [ 1]  / answers: 1 / hits: 17090  / 11 Years ago, tue, september 24, 2013, 12:00:00

I have two for loops on the second one I am using push to an array called myArray and it is not pushing the data has desired. Returning the array to the console in the second for loop outputs the following:



[Scottsdale CFS]
[Scottsdale CFS, Denver CFS]
[Warren CFS]
[Warren CFS, Millford CFS]
[Rockaway CFS]
[Rockaway CFS, Border CFS]


However, I would like the data to show like this:



[Scottsdale CFS, Denver CFS, Warren CFS, Millford CFS, Rockaway CFS, Border CFS]


How can I accomplish this?



note: The reason it is showing up like that is because I am iterating through a JSON file which checks through the first center and retrieves the data in an array and then goes to the next and does the same. The problem is that the arrays each have two elements which is why I am trying to push it into one array.



var looper = function(sec0, vz, lOrR) {                                

var myArray = [];

for(var i=0;i<vz[0]['Areas'].length;i++){
var tText = Object.keys(vz[0]['Areas'][i]);
var root = vz[0]['Areas'][i][tText][0];
var dataName;
}

var myArray = [];

if(sec0 === Centers) {

for(var j=0;j<root[sec0].length;j++){

var myString = root[sec0][j][Label];

myArray.push(myString);


charts.chart.renderTo = lOrR+myArray.indexOf(root[sec0][j][Label]);
charts.title.text = root[sec0][j][Label];
dataName = root[sec0][j]['Metrics'][5]['Rep Res. %'].slice(0,-1);
charts.series[0].name = dataName;
charts.series[0].data = [parseFloat(dataName)];
new Highcharts.Chart(charts);


}
}
}
}

More From » arrays

 Answers
160

The only reason is you are re declaring your array var myArray = [];



Try with following code,



var looper = function(sec0, vz, lOrR) {                                
var myArray = [];

for(var i=0;i<vz[0]['Areas'].length;i++){
var tText = Object.keys(vz[0]['Areas'][i]);
var root = vz[0]['Areas'][i][tText][0];
var dataName;
}
if(sec0 === Centers) {
for(var j=0;j<root[sec0].length;j++){
var myString = root[sec0][j][Label];
myArray.push(myString);
charts.chart.renderTo = lOrR+myArray.indexOf(root[sec0][j][Label]);
charts.title.text = root[sec0][j][Label];
dataName = root[sec0][j]['Metrics'][5]['Rep Res. %'].slice(0,-1);
charts.series[0].name = dataName;
charts.series[0].data = [parseFloat(dataName)];
new Highcharts.Chart(charts);
}
}
});

[#75464] Monday, September 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anitamaeg

Total Points: 466
Total Questions: 106
Total Answers: 106

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;