admin管理员组文章数量:1356965
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 acplish 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);
}
}
}
}
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 acplish 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);
}
}
}
}
Share
Improve this question
asked Sep 24, 2013 at 17:52
ClaudeClaude
4171 gold badge8 silver badges16 bronze badges
5
-
you are redeclaring
var myArray = [];
– Deepak Ingole Commented Sep 24, 2013 at 17:55 - there is an extraneous declaration of myArray – fred02138 Commented Sep 24, 2013 at 17:57
- Duh moment!!! That was it. Thanks Cap! – Claude Commented Sep 24, 2013 at 17:59
-
@Claude, I do not see how it could be it when you were not even referencing
myArray
between the first and second declaration... – plalx Commented Sep 24, 2013 at 18:01 - @plalx, I took out the second declaration and it worked. I even did some different tests and sure enough that was it. – Claude Commented Sep 24, 2013 at 18:06
1 Answer
Reset to default 4The 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);
}
}
});
本文标签: Javascript array push in a for loopStack Overflow
版权声明:本文标题:Javascript array push in a for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744065068a2584838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论