How to access data from a subArray with Javascript

Asked

Viewed 248 times

1

I need to access the data of a subarray via javascript, I can currently access the content of the entire array:

inserir a descrição da imagem aqui

What I need to take on the case is codigoImpressora that is inside the subarray impressoraPedido but I’m not finding anything to help me with that.

Contents of the Array:

{
"empresa":"testes",
"itensPedido":
[
{"impressoraPedido":
{"persistenceId":2,
"persistenceId_string":"2",
"persistenceVersion":0,
"persistenceVersion_string":"0",
"codigoImpressora":"2",
"codEmpresa":3,
"patrimonio":"1327",
"modeloImpressora":"T644",
"setorinstalacao":"Depósito peças"},
"suprimentoPedido":
{"persistenceId":3,
"persistenceId_string":"3",
"persistenceVersion":0,
"persistenceVersion_string":"0",
"codigoModeloImpressora":"2",
"codigoModeloSuprimento":2,
"nomeSuprimento":"TONER LEXMARK T644",
"codModeloSuprimento":"64418XL"},
"quantidadePedido":1}
]
} 

1 answer

4


It is only access in the hierarchy that the data meet.

Realize that itensPedido is a array, inside it has an object impressoraPedido and within this object is that you have what you need.

 
var obj = {  
   "empresa":"testes",
   "itensPedido":[  
      {  
         "impressoraPedido":{  
            "persistenceId":2,
            "persistenceId_string":"2",
            "persistenceVersion":0,
            "persistenceVersion_string":"0",
            "codigoImpressora":"2",
            "codEmpresa":3,
            "patrimonio":"1327",
            "modeloImpressora":"T644",
            "setorinstalacao":"Depósito peças"
         },
         "suprimentoPedido":{  
            "persistenceId":3,
            "persistenceId_string":"3",
            "persistenceVersion":0,
            "persistenceVersion_string":"0",
            "codigoModeloImpressora":"2",
            "codigoModeloSuprimento":2,
            "nomeSuprimento":"TONER LEXMARK T644",
            "codModeloSuprimento":"64418XL"
         },
         "quantidadePedido":1
      }
   ]
};

console.log(obj.itensPedido[0].impressoraPedido.codigoImpressora);

// Ou, se tiver mais de um item no array

for(let itemPedido of obj.itensPedido) {
    console.log(itemPedido.impressoraPedido.codigoImpressora);
}

  • This helped me in a way, but how can I do, for example when I have two lines, as I put in the image, in the example you gave worked correctly when you have a line but when you have two or more how I will be able to catch the object "password" respective of each row?

  • You commented right at the time I was editing, rs. Just look at the end of the answer.

  • He brought number 2 twice for what reason? Another thing, as I will have to display in the second dropdown only the respective content to the first dorpdown selected, as I could add each return of my check to a variable?

  • He showed it twice because he has two console.log - are two examples. I did not understand the second question, it is not the case of create a new question?

  • can be, thanks for the help so far :)

  • I have created a new question to try to explain my need to you now: https://answall.com/questions/209693/fill-in-content

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.