簡體   English   中英

在Typescript中這兩個數組賦值有什么區別?

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

我正在開發Angular 4應用程序。

我在我的應用程序中找到了以下代碼,但無法找到以下代碼的確切用途。

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

我將其更改為以下代碼,兩者都正常工作。

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

任何人都可以幫助我理解上面兩個代碼塊的區別。

有不同之處。 spread(...)運算符會銷毀數組並逐個返回元素,然后在[]中將它們再次組成一個數組。 這實際上是額外的操作。

所以this.auditList.filter(this.filterByRevisit)返回一個數組,這個[...this.auditList.filter(this.filterByRevisit)]返回一個展開的數組並再次生成一個數組。

我認為這兩者之間沒有區別。 ...會創建一個新數組, filter已經完成了。

但是如果我拿標題:

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