iterate json from http in typescript

To iterate through JSON received from an HTTP request in TypeScript, you can make use of the HttpClient provided by Angular. First, you would need to make the HTTP request to retrieve the JSON data. Once you have the data, you can parse it into a usable format and loop through it as needed.

index.ts
import { HttpClient } from '@angular/common/http';

constructor(private http: HttpClient) {}

getData() {
  this.http.get('https://example.com/data.json').subscribe(data => {
    for (let item of data['items']) {
      console.log(item);
    }
  });
}
252 chars
12 lines

In this example, we use the HttpClient to make a GET request to a JSON file hosted on a server. Once the data is retrieved, we parse it into an object using the JSON.parse() method. We then loop through the array of items inside the JSON object and log each item to the console.

Note that the subscribe() method is used to handle the asynchronous nature of the HTTP request.

gistlibby LogSnag