How to catch the Elements of a json?

Asked

Viewed 85 times

1

I have the return of a requisicao in the following format:

inserir a descrição da imagem aqui

There in options there are the types of cards the json returned. I want to get each of the types without manually typing their name.

Actually I can catch doing that:

response.paymentMethods.CREDIT_CARD.options.AMEX

But so I have to type in the name of each one. I want to count how many options you have and take them dynamically. Someone can help me?

1 answer

4


For what you’ve shown, response.paymentMethods.CREDIT_CARD.options brings the whole object.

I want to count how many options you have and take them dynamically.

I don’t know if I got it right, but you can get the flags by extracting the keys from the object:

var options = response.paymentMethods.CREDIT_CARD.options;
var bandeiras = Object.keys(options);
console.log(bandeiras.length + ' bandeiras disponíveis');

// Loop pelas chaves pegando os dados de cada bandeira
for(var i=0; i<bandeiras.length; i++) {
    console.log(options[bandeiras[i]]);
}
  • That’s just what I needed, it worked right here.

  • @Thiago If the answer solved the problem, be sure to mark.

Browser other questions tagged

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