【发布时间】:2019-06-14 22:45:53
【问题描述】:
这是两个对象:
const discount = {
rate: 0.50,
reason: 'New Year Sales'
}
const products = [
{
name: 'TV',
price: '1200'
},
{
name: 'Bed',
price: '500'
},
{
name: 'Table',
price: '50'
}
]
我想将它们放入一个新对象中:
const newProducts = [
{
name: 'TV',
price: '1200',
rate: 0.50,
reason: 'New Year Sales',
finalPrice: 600
},
{
name: 'Bed',
price: '500',
rate: 0.50,
reason: 'New Year Sales',
finalPrice: 250
},
{
name: 'Table',
price: '50',
rate: 0.50,
reason: 'New Year Sales',
finalPrice: 25
}
]
这是一个 JavaScript 练习,我尝试了“Object.assign”,但新值将覆盖旧值,导致只有最新的可用。我不确定使用 {...products} 和 map, forEach 进行解构是否有用。我无法做到这一点
【问题讨论】:
-
finalPrice 来自 2 个不同对象的 'rate * price'。我不知道如何执行对象的计算和创建。
-
听起来这是你的解决方案:stackoverflow.com/questions/20590177/…
标签: javascript json