Javascript - Access JSON object array

1703 views javascript
2

I have some JSON that looks like so:

data
  row_234745
    enqitem
       cost : "75.34"

It is stored in a data variable. I can access it in javascript like so:

console.log(data.data.row_234745.enqitem);

The problem is that row_234745 is variable. How can I make it so that the console displays the cost value without specifying the row?

I've tried things like:

console.log(data.data[0].enqitem);

But having no luck.

answered question

Still not getting you. Can you dump data exactly as it is in your code in the question

2 Answers

13

Try following

let row = "row_234745";
data.data[row].enqitem

posted this
10

Use bracket notation ([]) which allows you evaluate property dynamically:

Try

console.log(data.data[row_234745].enqitem);

posted this

Have an answer?

JD

Please login first before posting an answer.