Ashok Singh
Ashok Singh

Reputation: 243

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.

Upvotes: 2

Views: 80

Answers (2)

Danil Speransky
Danil Speransky

Reputation: 30453

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

Upvotes: 4

Suren Srapyan
Suren Srapyan

Reputation: 68665

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.

Upvotes: 5

Related Questions