[英]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.我需要为每个 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: ''}]
. [{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.返回值给您的 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: ""}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.