【发布时间】:2020-02-17 10:36:18
【问题描述】:
实际上,我想将帽子对象添加到服装对象,我不想将对象更改为对象数组。 谁能帮我在我的场景中添加帽子。
const clothing = {
shirts:{
id: 1,
title: 'shirts' ,
item: [
{
id: 01,
name:'plain shirt',
},
{
id: 02,
name: 'stripe shirt',
},
],
},
tshirt: {
id: 2,
title: 't-shirt',
item: [
{
id: 03,
name: 'plain t-shirt',
},
{
id: 04,
name: 'stripe t-shirt',
}
],
},
}
const newClothing = {...clothing};
newClothing[{hats:{id:3, title:'hats', item:[{id:05, name:'blue hats'}]}}];
console.log(newClothing.hats);
【问题讨论】:
-
clothing.hats = 你的帽子。
-
这能回答你的问题吗? Add new element to an existing object
-
{...clothing};一个小警告,这只会做一个浅拷贝,所以如果你期待一个完整的 newClothing 拷贝是行不通的。
标签: javascript json reactjs object