gpt4 book ai didi

javascript - 如何使用新对象添加嵌套对象

转载 作者:行者123 更新时间:2023-11-28 16:48:33 25 4
gpt4 key购买 nike

实际上,我想将帽子对象添加到服装对象,我不想将对象更改为对象数组。谁能帮助我如何在我的场景中添加帽子。

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);

最佳答案

这是最简单的方法:

const newClothing = {...clothing, hats: { ...your hats object here } };

关于javascript - 如何使用新对象添加嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60260610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com