简体   繁体   English

在Typescript中这两个数组赋值有什么区别?

[英]What is the difference between these two array assignments in Typescript?

I am working on Angular 4 application. 我正在开发Angular 4应用程序。

I found below code in my application but unable to find exact purpose of below code. 我在我的应用程序中找到了以下代码,但无法找到以下代码的确切用途。

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

I changed it to below code both are working fine. 我将其更改为以下代码,两者都正常工作。

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

Could any one help me to understand what is the difference in above two code blocks. 任何人都可以帮助我理解上面两个代码块的区别。

There is noting different. 有不同之处。 The spread (...) operator destroys the array and gives back the elements one by one and then in the [] put them into the making again an array. spread(...)运算符会销毁数组并逐个返回元素,然后在[]中将它们再次组成一个数组。 Which is actually extra operation. 这实际上是额外的操作。

So this.auditList.filter(this.filterByRevisit) returns an array, and this [...this.auditList.filter(this.filterByRevisit)] returns an array which is spread and again makes an array. 所以this.auditList.filter(this.filterByRevisit)返回一个数组,这个[...this.auditList.filter(this.filterByRevisit)]返回一个展开的数组并再次生成一个数组。

I don't think there is a difference between the two. 我认为这两者之间没有区别。 ... would create a new array, filter already did it. ...会创建一个新数组, filter已经完成了。

However if I take the title: 但是如果我拿标题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM