angular - ... 在对象数组之前的目的是什么
问题描述
我目前正在学习 Angular,并且发现了对我来说有点神秘的代码示例。
我有一个返回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
更多信息:https ://code4developers.com/spread-syntax-in-javascript/
推荐阅读
- c# - 如何在 Visual Studio 2015 的解决方案打开事件上创建 .txt 文件?
- python - 从 csv 读取最后 5 分钟
- pytorch - cuDNN 错误:使用天赋时 CUDNN_STATUS_EXECUTION_FAILED
- c - 从结构数组中交换两个结构(冒泡排序)
- mysql - nodejs从函数返回结果
- android - 在 NATIVE android 中滚动内容时使用自定义动画标记和区域焦点构建地图
- javascript - 即使操作成功,ajax post也返回404
- sql - 操作必须使用可更新查询
- google-cloud-platform - 谷歌云是否有类似“lambda@edge 原始响应”的东西
- python - 列表理解而不保存它与单行 for 循环