Search code examples
arraystypescriptangular5angular-changedetection

Assigning 3 dots argument to array in angular 5


I came across a line in Angular
What does it mean?

this.columns = [...this.columns, col];

I think this is related to immutable concept of array.


Solution

  • ... is a destructuring assignment in JavaScript, not only in Angular. It makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

    this.columns must be an object or array in order for it to be valid. It gets all the values or properties from this.columns and creates separate variables for each of them. From your code, its apparent that it most likely is an array.

    The code does this: Unpacks the current values from this.columns, creates a new array with these elements, and adding col, and assigns it to this.columns.

    In other words, that code adds col to this.columns.