How to access JSON return position with Javascript?

Asked

Viewed 278 times

0

How do I access some position of the array below depending on the user’s choice in a select? For example: if it selects option 11 on select i display the value of the key installmentAmount from position 11 to 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
  • I did, using the following: console.log(Response.installments[flag][value]. installmentAmount)

1 answer

2

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

objeto[índice]

In this your 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]));

Browser other questions tagged

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