create a object in array with products and values like pricr ccategory and namee. Use…
- User
create a object in array with products and values like pricr ccategory and namee. Use find method and filter methos with arrow funtion.
- Assistant
const products = [ { name: 'iPhone', price: 1000, category: 'Electronics' }, { name: 'Dress', price: 50, category: 'Fashion' }, { name: 'Keyboard', price: 30, category: 'Electronics' }, { name: 'Book', price: 20, category: 'Books' } ];
// Using find method with arrow function const foundProduct = products.find(product => product.name === 'iPhone'); console.log(foundProduct);
// Using filter method with arrow function const electronicProducts = products.filter(product => product.category === 'Electronics'); console.log(electronicProducts);
// Output: // { name: 'iPhone', price: 1000, category: 'Electronics' } // [ { name: 'iPhone', price: 1000, category: 'Electronics' }, // { name: 'Keyboard', price: 30, category: 'Electronics' } ]