1 回答
TA贡献1936条经验 获得超6个赞
既然要找到第一个匹配项,如果存在,则应该使用.find, not - 并且除了数组方法之外.filter不需要循环。for您还应该返回找到的对象的价格,而不是整个对象的价格。
function priceLookup(items, itemName) {
const found = items.find(item => item.itemName === itemName);
if (found) return found.price;
}
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 priceLookup(items, itemName) {
const found = items.find(item => item.itemName === itemName);
if (found) return found.price;
}
console.log(priceLookup(items, "Effective Programming Habits"));
添加回答
举报