access a json object as if it were an array

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 I have 'var arr = req.query.Detalhe;

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

It is not working, it appears undefined in the console;

    
asked by anonymous 02.08.2017 / 20:57

3 answers

1

You can use for..in (as you are doing), you just have to check the hasOwnProperty, to delete properties added to the prototype. Also check that the JSON received 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 over the properties keys and use them to access the properties:

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

Based on: link

    
02.08.2017 / 22:01
1

I manually arranged your Object ('DateVencer': '30082017' had a single quotation mark and ended up giving problem), make sure it is coming from server-side anyway, to iterate over 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);
}
    
02.08.2017 / 21:04
0

It worked as follows: FrontEnd the object looks 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);
}

Thanks for the help

    
02.08.2017 / 22:22