2017-08-30 132 views
2

我正在开发Angular 4应用程序。Typescript中这两个数组赋值有什么区别?

我在我的应用程序中找到了下面的代码,但无法找到下面代码的确切目的。

getManagementView(groupField: string) {   
    this.auditList = [...this.auditList.filter(this.filterByRevisit)]; 
    } 

我改变它为下面的代码都工作正常。

getManagementView(groupField: string) {   
    this.auditList = this.auditList.filter(this.filterByRevisit); 
    } 

任何人都可以帮助我理解上述两个代码块有什么区别。

+6

在我看来,有些人爱上了'...'运营商和滥用了很多。 – deceze

+0

标题中的问题与描述中的问题不同。 – trincot

回答

5

注意不同。 spread (...)运算符销毁数组并逐个返回元素,然后在[]中将它们再次放入数组中。这实际上是额外的操作。

所以this.auditList.filter(this.filterByRevisit)返回一个数组, 这[...this.auditList.filter(this.filterByRevisit)]返回其传播的数组,并再次使一个阵列。

4

我不认为两者有区别。 ...会创建一个新的数组,filter已经做到了。

但是,如果我拿冠军:

this.array = this.array  // does nothing, same object 
this.array = [...this.array] // creates a new array, though the same content