簡體   English   中英

使用擴展運算符向數組的每個 object 添加新屬性不起作用

[英]Adding new property to every object of array using spread operator does not work

我有一個帶有二進制文件的對象數組,像這樣

[{item: File},{item: File},{item: File}]

我需要為每個 obj 添加新的鍵/值。 我嘗試通過映射數組並將新屬性“描述”與現有“名稱”連接起來:

arr.map((el) => ({...el, description: '' }))

所以它應該是這樣的:

[{item: File, description: ''},{item: File, description: ''},{item: File, description: ''}]

但是當我嘗試這樣做時,它只返回一個沒有名稱的描述:

圖片

也許你忘了分配“地圖”

返回值給您的 object。 我們開始做吧:

let arr = [{name: '1'},{name: '2'},{name: '3'}]
arr = arr.map( item => ({ ...item, description:'' }) )
console.log(arr)

結果是:

0: {name: "1", description: ""}
1: {name: "2", description: ""}
2: {name: "3", description: ""}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM