access a json object as if it were an array

Asked

Viewed 1,314 times

0

I need to access a json object as if it were an array, Example I have the following object on the client side:

´    remessa : function (){
    var arr2 = [{
    'NrSequenciaDoc' :   '7777',  
    'TipoOcorrencia' :    '1',   
    'NossoNumero' : '66688', 
    'NumeroDocumento' : '6987',
    'DataVencimento' : '30082017', 
    'SacadoUF' : 'UF', },
    {
    'NrSequenciaDoc' :   '7777',  
    'TipoOcorrencia' :    '1',   
    'NossoNumero' : '66690', 
    'NumeroDocumento' : '6987',
    'DataVencimento' : '30082017', 
    'SacadoUF' : 'UF', }];


  axios({



    method: 'post',
    url: '/server/remessa',
    params: {
      Nrbanco : '085',
      TipoInscricao : '1',
      IncricaoCedente : '06624079975',
    ...
      Detalhe : arr2
    ...
      ,


    }

  }).then(function (response)
  {
    console.log('response.data' + response.data)
  }).catch(function (error) {
      console.log(error)
  })

}

and on the server and have `var arr = req.query.Detail;

for(i in arr){ console.log(arr[i]. Nossonumero); }`

Not working, appears Undefined on the console;

  • This JSON is invalid (OR OBJECT)

  • I wouldn’t have to change the arr[i] for detalhe[i]?

  • var arr receives Detahe before the var arr = req.query.Detail;

  • You want to loop inside each JSON block as if each block was an Array?

3 answers

1

I manually arranged your Object('Date Expiration' '30082017' had a simple quotes and ended up giving problem), check if it is coming anyway from the server-side, anyway, to iterate on an object use for(i in arr){ being arr its object and i the index.

var arr = [{
'NrSequenciaDoc' :   '7777',  
'TipoOcorrencia' :    '1',   
'NossoNumero' : '66688', 
'NumeroDocumento' : '6987',
'DataVencimento' : '30082017', 
'SacadoUF' : 'UF', },
{
'NrSequenciaDoc' :   '7777',  
'TipoOcorrencia' :    '1',   
'NossoNumero' : '66690', 
'NumeroDocumento' : '6987',
'DataVencimento' : '30082017', 
'SacadoUF' : 'UF', }];

for(i in arr){
console.log(arr[i].NossoNumero);
}

  • if I run this code directly on the Node server, it works, but if I send the same object on the front it doesn’t work, on the console it appears "Undefined" , and if I give a console.log(req.query.Detail) to check the object it comes as follows [ '{"NrSequenciaDoc":"7777","TipoOcorrencia":"1","NossoNumero":"66688","NumeroDocumento":"6987","DataVencimento":"30082017","SacadoUF":"UF"}',
 '{"NrSequenciaDoc":"7777","TipoOcorrencia":"1","NossoNumero":"66690","NumeroDocumento":"6987","DataVencimento":"30082017","SacadoUF":"UF"}' ]

  • You are processing this on JS or BACK-END(server-side)?

  • in Js send the request and in the server run the loop, in the front end I use Vue.js and Axios to make the request

  • what I need is to send this object through an http request and in the server do the lop on that object

  • But the loop you want to do in that language??

  • The loop will be done using the language of Node.js

Show 1 more comment

1


You can use for.. in (as you are doing) , just check hasOwnProperty to delete properties added to the prototype. Also check that the received JSON on the server is valid.

var object = {a:"1", b:"2", c:"3" }


for (var property in object) {
    if (object.hasOwnProperty(property)) {
        console.log(property
    }
}

Or iterate on the properties' Keys and use them to access the properties:

Object.keys(object).map(function(key) {
    console.log(key, object[key])
});

Based on : https://stackoverflow.com/questions/8312459/iterate-through-object-properties

  • This way I will only have access to the value {1,2 and 3}, but I also need the properties {a,b,c}. because I will have something in this sense Nossonumero = arr[i]. Nossonumero

  • If I understand your question correctly, the key variable will have the name of the property(a, b, c). if you do console.log(key) will have the property, and console.log(Object[key]), will have the value of it.

0

It worked the following way : Frontend the object was like this:

    var dadosbanco = '{"banco":[' +
    '{"NrSequenciaDoc":"7777","TipoOcorrencia":"1" ,"NossoNumero" : "66688", "NumeroDocumento" :"6987", "DataVencimento" : "30082017" },' +
    '{"NrSequenciaDoc":"7773","TipoOcorrencia":"2" ,"NossoNumero" : "66690", "NumeroDocumento" :"69898", "DataVencimento" : "30082019"  }' +
    ']}';

backend:

var jsonData = JSON.parse(req.query.Detalhe);
for (var i = 0; i < jsonData.banco.length; i++) {
    var counter = jsonData.banco[i];
    //console.log(counter.counter_name);
    console.log(counter.NrSequenciaDoc);
}

Grateful for the Help

Browser other questions tagged

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