... 对象数组之前的目的是什么
What is the purpose of the ... before array of the objects
我目前正在学习 Angular 并找到了对我来说有点神秘的代码示例。
我有一个函数 return 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]));
}));
}
在这个代码块中,有一个函数getSortedData
取[...products]
产品数组前的...
的目的是什么?
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;
}
});
}
Spread syntax allows an iterable such as an array expression or string
to be expanded in places where zero or more arguments (for function
calls) or elements (for array literals) are expected, or an object
expression to be expanded in places where zero or more key-value pairs
(for object literals) are expected.
来自MDN web docs Rest parameters:
The rest parameter syntax allows us to represent an indefinite number
of arguments as an array.
是Spread syntax
更多信息:https://code4developers.com/spread-syntax-in-javascript/
表示es6 spread operator
。
在您的情况下,它允许您传递 products
数组的 shallow 副本。
我目前正在学习 Angular 并找到了对我来说有点神秘的代码示例。
我有一个函数 return 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]));
}));
}
在这个代码块中,有一个函数getSortedData
取[...products]
产品数组前的...
的目的是什么?
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;
}
});
}
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
来自MDN web docs Rest parameters:
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
是Spread syntax
更多信息:https://code4developers.com/spread-syntax-in-javascript/
表示es6 spread operator
。
在您的情况下,它允许您传递 products
数组的 shallow 副本。