简体   繁体   中英

For loop prints only last value array in javascript

for (var i = 0; i < featureSet.features.length; i++) {
                 for (var f = 0, f1 = featureTracts.length; f < f1; f++) {

                     rows["Sensor"] = featureTracts[f].attributes.Sensor;
                     rows["Resolution"] = featureTracts[f].attributes.Resolution;
                     rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
                     // alert(rows);
                 }

                 resosat1[i] = rows;

             }

i am trying to print all values in resosat1[i] array but it will take only last value and all values overwirite and update only last value to array

for (var i = 0; i < featureSet.features.length; i++) {
             var rowAaaray = [];
             for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
                 var rows = {};
                 rows["Sensor"] = featureTracts[f].attributes.Sensor;
                 rows["Resolution"] = featureTracts[f].attributes.Resolution;
                 rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
                 // alert(rows);
              rowAaaray.push(rows);
             }

             resosat1[i] = rowAaaray;

         }

 }

Because you are maintaining one variable and overriding it in loop. So you will get last overwritten object only.

I agree with the guy in the comment. Try:

 for (var i = 0; i < featureSet.features.length; i++) { for (var f = 0, f1 = featureTracts.length; f < f1; f++) { rows["Sensor"] = featureTracts[f].attributes.Sensor; rows["Resolution"] = featureTracts[f].attributes.Resolution; rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW resosat1[f] = rows; <======== THIS WILL STORE THE VALUE OF EACH ROW CREATED } <==//TRY STORING THE resosat1 array In another array here instead. //eg. arrayEx[i]=resosat1 //Alternatively, you could use a 2D Array eg. arrayEx[i][f] } 

Hope this helps you out.

try this one..

      for (var i = 0; i < featureSet.features.length; i++) {
         var arr = []; // create array 
         for (var f = 0, f1 = featureTracts.length; f < f1; f++) {
             var rows = {};
             rows["Sensor"] = featureTracts[f].attributes.Sensor;
             rows["Resolution"] = featureTracts[f].attributes.Resolution;
             rows["Dtofparse"] = featureTracts[f].attributes.Dtofparse;//PATH_ROW
             // alert(rows);
          arr.push(rows);
         }

         resosat1[i] = arr;

     }
  }

rows is undeclared (at least in your snippet).

On the other hand, putting var rows = {} , as some suggest, will fix your problem because it creates new object each time. But, if your javascript version accepts it, it would be better to declare it wit let because, this way, you will be really creating new fresh variable (in block scope).

Declaring rows in an outer block will not fix your problem because you will be assigning same object during the whole loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM