這是我在這里的第一篇文章,所以我也感謝有關如何更好地發布的建議。我想知道如何在控制器中使用過濾器?
我有這個類/控制器來添加產品:
class ProductController {
constructor() {
this.allProductItems = [];
}// end of constructor
//methods
addProduct(product, productName, productID, productPrice, productDescription, productTrivia, productImg, recoDelivery, productRemarks, productCategory){
const productItem = {
Product: product,
name: productName,
id: productID,
price: productPrice,
desc: productDescription,
trivia: productTrivia,
img: productImg,
recoDelivery: recoDelivery,
remarks: productRemarks,
category: productCategory,
} //end of productitem
this.allProductItems.push(productItem);
console.log(this.allProductItems)
} // end of addproduct
然后我有一個類的實體,它是:
const products = new ProductController();
products.addProduct("Lemongrass Bodywash", "Bodywash, Lemongrass, 500ml", "04", 25, "Treat your skin and let it heal after a long day. Rinse away the dirt and grime of life with the scent of lemongrass and natural oils.", "From 1936 to 1950, a cartel of motor and oil companies successfully conspired to replace rail transport in Los Angeles with buses.", "images/menbodywash.jpg", 2, "E", "F");
我想根據價格 > 25 進行過濾。
我試過這個但它不起作用:
const filterProductItems = this.allProductItems.filter(productEx => productEx.productPrice > 50);
console.log(filterProductItems);
提前致謝!
uj5u.com熱心網友回復:
嘗試更改您的代碼。
const filterProductItems = this.allProductItems.filter(productEx => productEx.price > 25);
uj5u.com熱心網友回復:
我認為問題在于您正在檢查錯誤的密鑰
productPrice代替price
你的代碼似乎作業正常
class ProductController {
constructor() {
this.allProductItems = [];
} // end of constructor
//methods
addProduct(product, productName, productID, productPrice, productDescription, productTrivia, productImg, recoDelivery, productRemarks, productCategory) {
const productItem = {
Product: product,
name: productName,
id: productID,
price: productPrice,
desc: productDescription,
trivia: productTrivia,
img: productImg,
recoDelivery: recoDelivery,
remarks: productRemarks,
category: productCategory,
} //end of productitem
this.allProductItems.push(productItem);
console.log(this.allProductItems)
} // end of addproduct
}
const products = new ProductController();
products.addProduct("Lemongrass Bodywash", "Bodywash, Lemongrass, 500ml", "04", 25, "Treat your skin and let it heal after a long day. Rinse away the dirt and grime of life with the scent of lemongrass and natural oils.", "From 1936 to 1950, a cartel of motor and oil companies successfully conspired to replace rail transport in Los Angeles with buses.", "images/menbodywash.jpg", 2, "E", "F");
console.log(products.allProductItems.filter(p => p.price > 25))
uj5u.com熱心網友回復:
試試這個。希望這對你有用。
class ProductController {
constructor() {
this.allProductItems = [];
}// end of constructor
//methods
addProduct(product, productName, productID, productPrice, productDescription, productTrivia, productImg, recoDelivery, productRemarks, productCategory){
const productItem = {
Product: product,
name: productName,
id: productID,
price: productPrice,
desc: productDescription,
trivia: productTrivia,
img: productImg,
recoDelivery: recoDelivery,
remarks: productRemarks,
category: productCategory,
} //end of productitem
this.allProductItems.push(productItem);
// console.log(this.allProductItems)''
} // end of addproduct
}
const products = new ProductController();
products.addProduct("Lemongrass Bodywash", "Bodywash, Lemongrass, 500ml", "04", 25, "Treat your skin and let it heal after a long day. Rinse away the dirt and grime of life with the scent of lemongrass and natural oils.", "From 1936 to 1950, a cartel of motor and oil companies successfully conspired to replace rail transport in Los Angeles with buses.", "images/menbodywash.jpg", 2, "E", "F");
products.addProduct("Coke", "Bodywash, Lemongrass, 500ml", "04", 100, "Treat your skin and let it heal after a long day. Rinse away the dirt and grime of life with the scent of lemongrass and natural oils.", "From 1936 to 1950, a cartel of motor and oil companies successfully conspired to replace rail transport in Los Angeles with buses.", "images/menbodywash.jpg", 2, "E", "F");
console.log(products.allProductItems.filter(product => product.price > 25));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/475506.html
標籤:javascript 数组 筛选 控制器
上一篇:按ID查找和更新嵌套陣列元素
下一篇:洗掉3個div內的只讀屬性
