简体   繁体   中英

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

I am working on Angular 4 application.

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. 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.

I don't think there is a difference between the two. ... would create a new array, filter already did it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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