要高效地从对象数组中找出最小值对应的对象,推荐使用 lodash 的 _.minby 方法或原生 javascript 的 reduce 方法。1. 使用 lodash 的 _.minby:可直接传入数组和属性名(或函数)来获取最小值对象,语法简洁;2. 使用 array.prototype.reduce():通过一次遍历比较每个元素,灵活性高且无需依赖外部库;3. 使用 for...of 循环:性能最优但代码较冗长,适合对性能要求极高的场景;4. 避免使用 array.prototype.sort() 仅为了找最小值,因其时间复杂度较高,效率较低。最终答案是:推荐优先使用 reduce 或 _.minby 实现高效查找。

在JavaScript中,要从一个对象数组里根据某个特定属性找出最小值对应的对象,
minBy
当我们需要从一个对象数组中,基于某个属性的值来找出最小值对应的整个对象时,
minBy
使用 Lodash 的 _.minBy
_.minBy
import _ from 'lodash';
const products = [
{ name: 'Laptop', price: 1200, stock: 10 },
{ name: 'Mouse', price: 25, stock: 50 },
{ name: 'Keyboard', price: 75, stock: 20 },
{ name: 'Monitor', price: 300, stock: 5 }
];
// 根据 price 属性找到价格最低的产品
const cheapestProduct = _.minBy(products, 'price');
console.log(cheapestProduct);
// 输出: { name: 'Mouse', price: 25, stock: 50 }
// 如果需要更复杂的逻辑,可以使用函数作为迭代器
const cheapestProductByCalculatedValue = _.minBy(products, product => product.price / product.stock);
console.log(cheapestProductByCalculatedValue);
// 输出: { name: 'Mouse', price: 25, stock: 50 } (因为 25/50 = 0.5 是最小值)原生 JavaScript 实现 minBy
Array.prototype.reduce()
minBy
const products = [
{ name: 'Laptop', price: 1200, stock: 10 },
{ name: 'Mouse', price: 25, stock: 50 },
{ name: 'Keyboard', price: 75, stock: 20 },
{ name: 'Monitor', price: 300, stock: 5 }
];
// 模拟 minBy 逻辑,根据 price 属性
const getMinBy = (arr, iteratee) => {
if (!arr || arr.length === 0) {
return undefined; // 或者 null,根据你的错误处理偏好
}
return arr.reduce((minItem, currentItem) => {
const minVal = typeof iteratee === 'function' ? iteratee(minItem) : minItem[iteratee];
const currentVal = typeof iteratee === 'function' ? iteratee(currentItem) : currentItem[iteratee];
return currentVal < minVal ? currentItem : minItem;
});
};
const cheapestProductNative = getMinBy(products, 'price');
console.log(cheapestProductNative);
// 输出: { name: 'Mouse', price: 25, stock: 50 }
const cheapestProductByCalculatedValueNative = getMinBy(products, product => product.price / product.stock);
console.log(cheapestProductByCalculatedValueNative);
// 输出: { name: 'Mouse', price: 25, stock: 50 }这个原生实现虽然代码量多一点,但它能让你更清楚地看到比较和迭代的过程,而且在没有 Lodash 的环境下,它就是你的
minBy
当谈到从对象数组中找出最小值,我们有很多选择,效率和代码可读性往往是需要权衡的两个点。除了上面提到的
minBy
reduce
1. 使用 Array.prototype.reduce()
reduce
const items = [{ id: 1, value: 10 }, { id: 2, value: 5 }, { id: 3, value: 12 }];
const minItem = items.reduce((prev, current) => {
return (prev.value < current.value) ? prev : current;
});
console.log(minItem); // { id: 2, value: 5 }这种方法非常灵活,你可以轻松修改比较逻辑,比如处理
null
undefined
2. 使用 for...of
const items = [{ id: 1, value: 10 }, { id: 2, value: 5 }, { id: 3, value: 12 }];
let minItemLoop = null;
if (items.length > 0) {
minItemLoop = items[0];
for (let i = 1; i < items.length; i++) {
if (items[i].value < minItemLoop.value) {
minItemLoop = items[i];
}
}
}
console.log(minItemLoop); // { id: 2, value: 5 }这种方式的缺点是,你需要手动处理空数组的情况,而且初始化
minItemLoop
3. 使用 Array.prototype.sort()
sort()
以上就是js 怎样用minBy获取对象数组的最小值的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号