How to show only 20 object from Array in typescript?

Tags: ,



I am using angular 7. I have initialized an array given as:

 cacheDatas=[];

Here cacheData has 1000 of object which are initialized to cacheDatas but I only need 20 object.

getDataOfCache(cacheData:any){
       this.cacheDatas=cacheData;
       this.cacheDatas.slice(0,20);
     console.log(this.cacheDatas);
  }

I tried to implement the slice method but it is not working. The value cacheData:any is: enter image description here

Answer

Need to assign the value for the variable: cacheDatas after slice.

this.cacheDatas = this.cacheDatas.slice(0,20);

Example here: https://stackblitz.com/edit/angular-3d4ypz



Source: stackoverflow