I am currently learning Angular and have found example of the code that is a bit mysterious for me.
I have a function that return Observable<Product[]>
array of objects:
connect(): Observable<Product[]> {
const dataMutations = [
this.productsSubject,
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(map((products) => {
this.paginator.length = products.length;
return this.getPagedData(this.getSortedData([...products]));
}));
}
In this code block, there is the function getSortedData
that takes [...products]
what is purpose of the ...
before the array of products?
Code sample of the getSortedData
:
private getSortedData(data: Product[]) {
if (!this.sort.active || this.sort.direction === '') {
return data;
}
return data.sort((a, b) => {
const isAsc = this.sort.direction === 'asc';
switch (this.sort.active) {
case 'title': return compare(a.title, b.title, isAsc);
default: return 0;
}
});
}