0
我等給出一類對象獲得第4項的任何數組或類:如何基於按升序排列它們的價格條目
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' }
];
在打字稿我想獲取從它的項目的給定數量基於最低價格。該函數參數提供了該提取的數字。
getCheapestProduct(id:number): Product[] {
return this.Products; // will have to return the first few elements/
//(specified by id) in ascending prices.
}
請幫忙!
您可以通過提供回調函數對對象數組進行排序。看到這裏:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Matey
這與TypeScript無關。你必須對產品進行排序,然後得到第一個'n'元素。這兩項任務都應該可以通過在SO或您的首選搜索提供商上進行簡短搜索來解決。 – Andreas
@Matey這是一個類對象的數組而不是數組的字符串。 – yogihosting