Currently delving into Angular and stumbled upon a code snippet that seems a bit cryptic to me.
The function I'm working with returns an array of objects as Observable<Product[]>
:
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]));
}));
}
Within this code snippet, there is a function called getSortedData
that takes [...products]
, what is the significance of the ...
before the array of products?
Here's a glimpse of the getSortedData
function:
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;
}
});
}