PabloPhones
PabloPhones

Reputation: 123

amount of returned values - REST API angular

I am using regular REST API with Angular like under the link: https://www.techiediaries.com/angular-by-example-httpclient-get/

In the function:

ngOnInit() {

    this.dataService.sendGetRequest().subscribe((data: any[])=>{
      console.log(data);
      this.products = data;
    })  
  }

can we get into data and get to know how many elements is returned?

I need to know how many elements are returned from backend.

Upvotes: 0

Views: 251

Answers (2)

Simon Echle
Simon Echle

Reputation: 296

If data is an array simply use length property.

ngOnInit() {

    this.dataService.sendGetRequest().subscribe((data: any[])=>{
      console.log(data);
      this.products = data;
      this.productsLength = data.length;
    })  
  }

Upvotes: 0

Maihan Nijat
Maihan Nijat

Reputation: 9355

The API returns array data: any[]. Javascript Array has length property which shows the total number of items in the array. All you need is to use data.length. If you are dealing with data, then you probably need to know the basics of Array: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Upvotes: 2

Related Questions