Javascript 在使用reduce时,要努力使用js语法
在以下方面:Javascript 在使用reduce时,要努力使用js语法,javascript,reduce,Javascript,Reduce,在以下方面: let items = [ { itemName: "Effective Programming Habits", type: "book", price: 13.99 }, { itemName: "Creation 3005", type: "computer", price: 299.99 }, { ite
let items = [
{
itemName: "Effective Programming Habits",
type: "book",
price: 13.99
},
{
itemName: "Creation 3005",
type: "computer",
price: 299.99
},
{
itemName: "Finding Your Center",
type: "book",
price: 15.00
}
]
function myFunction(max, {price}) {
if (price > max) {
return price
}
return max
}
为什么{price}返回每个对象的价格值?为什么不选择item['price']或item.price?你在文档中找到了什么答案?你在寻找什么?每当你需要查看js文档时,我认为MDN是最好的答案,请查看数组部分的exmample 您可以尝试以下方法:
const highestPrice = (myArray) => {
let max = 0; //variable to return
for(let i = 0; i < myArray.length; i++){
if(myArray[i].price > max){ //check current element
max = myArray[i].price;
}
}
return max;
}
const highestPrice=(myArray)=>{
设max=0;//要返回的变量
for(设i=0;imax){//检查当前元素
max=myArray[i]。价格;
}
}
返回最大值;
}
您应该在代码段中包含reduce调用。此外,除非您以后需要重新分配,否则使用let
也不是一个好的做法。在这种情况下,只需最高价格299.99。希望这是您正在寻找的解决方案。快乐编码!