我试图在下面的amusementRides
函数中遍历doublePrices
,将价格乘以2,但没有任何回报。我想念什么?
var amusementRides = [
{
id:'A1',
name:'superman',
price:12,
opening_dates:[
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday'
],
children_access:false
},
{
id:'A2',
name:'raging bull',
price:10,
opening_dates:[
'Saturday',
'Sunday'
],
children_access:false
},
{
id:'A3',
name:'teacups',
price:7,
opening_dates:[
'Friday',
'Saturday',
'Sunday'
],
children_access:true
}
];
// Define variable
var amusementRidesDouble = doublePrices(amusementRides);
// Define function to multiply price by 2
function doublePrices(amusementRides) {
for (var price in amusementRides) {
if amusementRides.hasOwnProperty(price) {
alert (price *= 2);
}
}
}
console.log(amusementRidesDouble);
提前致谢!
最佳答案
您可以使用数组的映射功能并更新必填字段
function doublePrices(amusementRides) {
amusementRidesdouble = amusementRides.map(function(item){
item.price = 2 * item.price;
return item;
})
return amusementRidesdouble;
}