-4

I am very new to angular and I am assigned to a task where I want to get data from an API and iterate and display it in a chart. My angular version is 6.

This is the response JSON format, It is a list of objects-

[
    {
        "counts": 0,
        "time": "00:00:00"
    },
    {
        "counts": 0,
        "time": "01:00:00"
    },
    {
        "time": "02:00:00",
        "counts": 1
    },
    {
        "counts": 7,
        "time": "03:00:00"
    }
]

To fetch that

 data : [];

this.service.getPaymentDistributionData()
    .subscribe((res) => {
      console.log(res)
    })

How can I get each count and time and print?

Thanks,

Dasun
  • 602
  • 1
  • 11
  • 34
  • 1
    Does this answer your question? [Iterate over array of objects in Typescript](https://stackoverflow.com/questions/46213989/iterate-over-array-of-objects-in-typescript) – Beller Aug 31 '21 at 09:49
  • If i use of it is saying like Type 'Object' is not an array type or a string type – Dasun Aug 31 '21 at 10:22

2 Answers2

1

You can use for .. of

for (let ct of res) {
     console.log(ct.counts);
     console.log(ct.time); 
}
user1234
  • 91
  • 8
1
this.service.getPaymentDistributionData()
.subscribe((res) => {
    res.forEach(x => console.log(x.count))
})

Try Something Like this For Array of Object where res = [{}, {}]

for Object where res = {}

Object.keys(res).forEach((key) => )
tiana
  • 374
  • 1
  • 8
  • 23