Jarred Ti
Jarred Ti

Reputation: 21

How do I ignore the 1st record in a table when using the get and subscribe using Angular?

How do I ignore the 1st record in a table when using the get and subscribe using Angular? using the code below:-

getAdmins() {

    this.data = [];
    this.api.getAdminList()
      .subscribe(
        data => {
          this.data = data;

          if (this.isFirstLoad) {
            this.isFirstLoad = false;
            this.dtTrigger.next();
          }
        }
      );
  }

My data layout as follow:-

I want to ignore the Administrator record but start to display the record from Mr x onward.

Upvotes: 2

Views: 66

Answers (4)

Lahiru Mirihagoda
Lahiru Mirihagoda

Reputation: 1163

You can try using Array splice

    this.api.getAdminList()
      .subscribe(
        data => {
          if(data.length > 0) {
            this.data= data.splice(0,1);
          }
        });

or try reversing the array and then popping the last item

    this.api.getAdminList()
      .subscribe(
        data => {
          if(data.length > 0) {
            this.data = data.reverse().pop();
          }
        });

Upvotes: 1

Mohamed Farouk
Mohamed Farouk

Reputation: 1107

getAdmins() {
    this.data = [];
    this.api.getAdminList()
      .subscribe(
        data => {
          this.data = data.shift(); //The shift() command will remove the first element of the array
        }
      );
}

Upvotes: 1

JossFD
JossFD

Reputation: 2216

Try:

this.api.getAdminList().pipe()
  .subscribe(
    data => {
      this.data = data.splice(1);
    }
  );

Upvotes: 0

r0secr01x
r0secr01x

Reputation: 224

Have you thought about using splice?

this.data = [];
this.api.getAdminList()
  .subscribe(
    data => {
      this.data = data.length > 1 ? data.splice(0, 1) : [];
    }
  );

https://www.w3schools.com/jsref/jsref_splice.asp

Upvotes: 0

Related Questions