How to access JSON return position with JavaScript?

0

How do I access some position in the array below depending on the user's choice in select ? For example, if he selects option 11 in select I display the value of the installmentAmount key of position 11 for the user.

{error: false, installments: {…}}
    error:false
    installments:mastercard:Array(12)
    0:{quantity: 1, installmentAmount: 200.5, totalAmount: 200.5, interestFree: true}
    1:{quantity: 2, installmentAmount: 100.25, totalAmount: 200.5, interestFree: true}
    11:{quantity: 12, installmentAmount: 18.77, totalAmount: 225.18, interestFree: false}
    length:12
    
asked by anonymous 16.04.2018 / 22:47

1 answer

2

The access form of an object in this case is equivalent to an array:

objeto[índice]

In this example:

var obj = {error: false, installments: {},
    error:false,
    installments:'',
    0:{quantity: 1, installmentAmount: 200.5, totalAmount: 200.5, interestFree: true},
    1:{quantity: 2, installmentAmount: 100.25, totalAmount: 200.5, interestFree: true},
    11:{quantity: 12, installmentAmount: 18.77, totalAmount: 225.18, interestFree: false},
    length:12
}
  
document.write(JSON.stringify(obj[11]));
    
16.04.2018 / 23:04