简体   繁体   中英

how do I assign values to a variable taking them from JSON using a nested for loop?

I have a JSON with both subject fields and their corresponding values separately. I want to write a nested for loop that will assign the value to each subject field from below json. Right now, I am using a single for loop to loop through all the values by defining the subject field. I will be using sqlcmd-insert in each for loop after I assign the values to subjects.

for(var i = 0; i < obj.value.length; i++){
const Name= mm[i][0];
const Age= mm[i][1];
const Country= mm[i][2];
const Gender= mm[i][3];
const IsActive= mm[i][3];
}

I don't want to define variable names, instead I want it to be taken directly from JSON subject field and assign the corresponding value. I tried below nested loop but it doesn't work. How can I make this work? Please help.

for(var i = 0; i < obj.value.length; i++)
      {
        for(var j = 0; j < obj.subject.length; j++)
        {
          const obj.subject[i].field = obj.value[i][j];
        }
      }

 { "First": 1, "Last": 3, "msg": "", "subject": [ { "col": 0, "field": "Name" }, { "col": 1, "field": "Age" }, { "col": 2, "field": "Country" }, { "col": 3, "field": "Gender" }, { "col": 4, "field": "IsActive" } ], "value": [ [ "Sam", 30, "US", "Male", "Y" ], [ "Tom", 32, "UK", "Male", "Y" ], [ "Kate", 28, "USA", "Female", "N" ] ] }

 for(var i = 0; i < obj.value.length; i++) { obj.subject[0].name=obj.value[i][0]; }

 for(var i = 0; i < obj.value.length; i++) { obj.subject[0].field=obj.value[i][0]; obj.subject[0].field=obj.value[i][1]; obj.subject[0].field=obj.value[i][2]; obj.subject[0].field=obj.value[i][3]; obj.subject[0].field=obj.value[i][4]; }

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