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];
}