最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript array push in a for loop - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

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);
        }                                            
    }
});
发布评论

评论列表(0)

  1. 暂无评论