1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Using slice() method while initializing array in constructor, Angular Material, Typescript...

Discussão em 'Angular' iniciado por Prateek Tewary, Setembro 12, 2024.

  1. Going through angular material table component I found that the data of the table is stored in data variable and that is assigned in the constructor (which is fine). Then I observed that the data is being sliced before assigning. I couldn't find a valid explanation why this is being done!

    desserts: Dessert[] = [
    {name: 'Frozen yogurt', calories: 159, fat: 6, carbs: 24, protein: 4},
    {name: 'Ice cream sandwich', calories: 237, fat: 9, carbs: 37, protein: 4},
    {name: 'Eclair', calories: 262, fat: 16, carbs: 24, protein: 6},
    {name: 'Cupcake', calories: 305, fat: 4, carbs: 67, protein: 4},
    {name: 'Gingerbread', calories: 356, fat: 16, carbs: 49, protein: 4},
    ];

    sortedData: Dessert[];

    constructor() {
    this.sortedData = this.desserts.slice();
    }



    It can be found here

    I tried to use console and declare the desserts array and use slice method just to be sure. But it is not making any difference. I want to know why it is done like that. What difference it makes?

    Continue reading...

Compartilhe esta Página