简体   繁体   中英

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

I have an array of objects with binary Files like this

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

and i need to add new key/value to every obj. I tried doing it with mapping an array and concatenating new property 'description' with existing 'name':

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

so it should look like this:

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

But when i try to do this, it only returns a description without name:

图片

maybe you forgot to assign 'map'

returned value to your object. lets do it:

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

result is:

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

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