How to get the elements of a json?

1

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

In there are the types of cards json has returned. I want to get each type without manually typing their names.

Currently I can get it by doing this:

response.paymentMethods.CREDIT_CARD.options.AMEX

But I need to type the name of each one. I want to count how many options you have and get them dynamically. Can someone help me?

    
asked by anonymous 03.04.2018 / 22:33

1 answer

4

From what you have shown, response.paymentMethods.CREDIT_CARD.options brings the whole object.

  

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

I do not know if I understand correctly, 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]]);
}
    
03.04.2018 / 22:38