0

I have following json array. I want to access the value of revenue for all of them.

data = [
    {"year": 2017, "revenue": 1000, "cost": 500},
    {"year": 2016, "revenue": 1500, "cost": 900},
    {"year": 2015, "revenue": 900, "cost": 300}
]

I can get data[0].revenue. But it only give me access to one year. How can I access them all? I'm really new to javascript.

2
  • 3
    data.forEach(yearObject => console.log(yearObject.revenue)); Commented Apr 26, 2018 at 3:55
  • Do enough research before asking Commented Apr 26, 2018 at 3:58

1 Answer 1

3
for (var i=0;i<data.length;i++){
  var record=data[i];
  console.log(record.year, record.revenue, record.cost);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.