YogeshChauhan . com

How to convert an object from API to JSON array in Angular 9?

in Angular on May 16, 2020

If you're getting the data using API, it will be easier to use an array to display data rather than an object.

Declare your array first in your component class:


...
array: [];
...

Then use JSON.stringify and JSON.parse to convert the object into an array.


...
...
ngOnInit()    {
    this.service.method()
    .subscribe(
        data=>
      {
        this.array = JSON.parse(JSON.stringify(data.object));
      }
    )
}
...

You can use that array to print your results from API data in html template.

For e.g.


<p>{{array['something']}}</p>
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

What is a component helper function in React?ReactArray.from() Method in JavaScriptJavaScriptIs there a difference between SCSS and Sass?SCSSSome Useful Operators in The SQL WHERE ClauseSQL/MySQLWhat is the correct way to enqueue multiple CSS files in WordPress?WordPressCSS align-items property examplesCSSHow to change CSS with jQuery?CSSHow to create ‘share on LinkedIn’ link using just HTML?HTMLHow to create a simple drop down menu using JavaScript and CSS?CSSWhat does it mean by Continuous Integration, Continuous Delivery and Continuous Deployment?Misc$this Keyword in PHPPHPWhy does MOV matter in IT or any kind of projects?Misc