this is my first post here so I also appreciate advice on how to post better. I'm wondering how do I use filters inside a controller?
I have this class/controller to add products:
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
Then I have an instance of the class which is:
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");
I want to filter according to say, price > 25.
I tried this but it does not work:
const filterProductItems = this.allProductItems.filter(productEx => productEx.productPrice > 50);
console.log(filterProductItems);
Thanks in advance!
CodePudding user response:
try to change your code.
const filterProductItems = this.allProductItems.filter(productEx => productEx.price > 25);
CodePudding user response:
I think that the problem is that you are checking for the wrong key
productPrice
instead of price
your code seems to work fine
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))
CodePudding user response:
Try this. Hope this will work for you.
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));