📜  从 javascript 代码示例中的对象获取最便宜的电话

📅  最后修改于: 2022-03-11 15:03:28.897000             🧑  作者: Mango

代码示例1
const phones = [
    { name: 'samsung s5', price: 45000, camera: 10, storage: 32 },
    { name: 'walton', price: 15000, camera: 8, storage: 8 },
    { name: 'shaomi m3', price: 12000, camera: 10, storage: 16 },
    { name: 'oppo a2', price: 17000, camera: 10, storage: 32 },
    { name: 'Nokia n95', price: 8000, camera: 10, storage: 4 },
    { name: 'htc h81', price: 25000, camera: 10, storage: 16 }
]
let cheapest = phones[0];
for (let phone of phones) {
    if (phone.price < cheapest.price) {
        cheapest = phone;
    }
}
console.log(cheapest);
//{ name: 'Nokia n95', price: 8000, camera: 10, storage: 4 }