gpt4 book ai didi

javascript - 如何将一个对象附加到另一个对象?

转载 作者:行者123 更新时间:2023-12-01 15:30:39 25 4
gpt4 key购买 nike

有人可以帮我将一个对象附加到另一个对象吗?例如,我有原始对象:

var object = {
{ product_code: '55993',
brand: 'Phillips66',
category: 'LUBES AND GREASES',
description: 'cuatro cinco seis',
price: '300.00',
quantity: '3'
}
}

我想附加:
    { product_code: '90210',
brand: 'Phillips66',
category: 'LUBES AND GREASES',
description: 'cuatro cinco seis',
price: '500.00',
quantity: '5'
}

所以最后, object好像:
object = {
{ product_code: '55993',
brand: 'Phillips66',
category: 'LUBES AND GREASES',
description: 'cuatro cinco seis',
price: '300.00',
quantity: '3'
},
{ product_code: '90210',
brand: 'Phillips66',
category: 'LUBES AND GREASES',
description: 'cuatro cinco seis',
price: '500.00',
quantity: '5'
}
}

我怎么做?

最佳答案

您可以使用 ES6 扩展运算符来附加对象。

object = { ...object, 
...{ product_code: '90210',
brand: 'Phillips66',
category: 'LUBES AND GREASES',
description: 'cuatro cinco seis',
price: '500.00',
quantity: '5'
}
}

关于javascript - 如何将一个对象附加到另一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35496532/

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