Let's say we have an array of objects representing products:
Products: Product[] = [
{ id: 1, name: 'Milk', price: '1' },
{ id: 2, name: 'Flour', price: '20' },
{ id: 3, name: 'Jeans', price: '29' },
{ id: 4, name: 'T-Shirts', price: '14.59' },
{ id: 5, name: 'Purse', price: '199.99' },
{ id: 6, name: 'Jacket', price: '200' },
{ id: 7, name: 'Football', price: '50' },
{ id: 8, name: 'Pens', price: '5' },
{ id: 9, name: 'Tooth Brush', price: '4' },
{ id: 10, name: 'lipistic', price: '9.89' }
];
Now, in TypeScript, we need to create a function that will fetch a certain number of items with the lowest prices from this list. The number of items to fetch will be provided as a parameter to the function.
getCheapestProduct(id:number): Product[] {
return this.Products; // Need to return the first few elements (specified by id) in ascending order of their prices.
}
Your assistance on this matter would be greatly appreciated!