Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through a JSON object with typescript (Angular2) [duplicate]

Tags:

I am new to Angular2 and I am trying to loop through a JSON object that I am getting back from a GET request but can't work it out.

My JSON object:

{     Results: [{         Time: "2017-02-11T08:15:01.000+00:00",         Id: "data-mopdsjkajskda",         AuthorId: "58fSDNJD"     }, {         Time: "2017-03-11T06:23:34.000+00:00",         Id: "data-2371212hjb1",         AuthorId: "43555HHHJ"     }, {         Time: "2017-04-11T07:05:11.000+00:00",         Id: "data-kjskdha22112",         AuthorId: "XDSJKJSDH"     }] } 

Part of my Angular script:

interface res {     Time: string;     Id: string;     AuthorId: string; } export class AppComponent {     results: res;     constructor(private _httpservice: HTTPService) {}     this._httpservice.getQuery().subscribe(         data => {             this.results = data.Results         },         error => console.log(error),         () => console.log('Done')     ); } 

I do get the data back - which is great. However, I want to push the Ids into an array. In Javascript I would do this:

var ids = [];  for (i = 0; i < data.Results.length; i++) {     ids.push(data.Results[i].Id) } 

The array after the push:

ids = ['data-mopdsjkajskda', 'data-2371212hjb1', 'data-kjskdha22112']; 

I am struggling to find a way to achieve the same results with Angular2. Any help would be greatly appreciated!

like image 308
AlexisP Avatar asked May 01 '17 20:05

AlexisP


People also ask

Can we loop through JSON object?

Looping Using JSON JSON stands for JavaScript Object Notation. It's a light format for storing and transferring data from one place to another. So in looping, it is one of the most commonly used techniques for transporting data that is the array format or in attribute values.

How do you loop a JSON response?

Use Object.values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.

How to convert/parse JSON to/from a JavaScript object in Angular application?

How to Convert/parse JSON to/from a javascript object in Angular application. 1 JSON.stringify () method string version of an object, which is the conversion of an object to JSON string 2 JSON.parse () - parse string JSON object and creates javascript object More ...

What is the difference between JSON and typescript in angular?

Typescript is the typed language used to check type check at compile time. So in Angular, Each variable must hold a type to specify the type of the type. JSOn is normal data, So declare a variable for any type. any type in typescript enables to accept any type of data.

How do I loop through a JSON response in JavaScript?

We can do this with JSON.parse (): Once we have our response as a JavaScript object, there are a number of methods we can use to loop through it. A for…in loop iterates over all enumerable properties of an object:

How to declare and assign new types in typescript in angular?

In the Angular component, Declare a variable of an Employee array and assign json object. type keyword allows to declare and assign new types in typescript. You can check more about [typescript type] (/typescript-type-keyword/


1 Answers

Assuming your json object from your GET request looks like the one you posted above simply do:

let list: string[] = [];  json.Results.forEach(element => {     list.push(element.Id); }); 

Or am I missing something that prevents you from doing it this way?

like image 177
csim Avatar answered Oct 02 '22 19:10

csim