Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

92
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda