Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

91
Views
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"
    ]
  ]
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!