minBy()
minBy<
T>(array,iteratee):T
minBy<
T>(array,iteratee):T|undefined
Returns the element with the minimum value computed by iteratee.
Type Parametersβ
T: Tβ
The type of elements in the array.
Parametersβ
Overload 1:
array: readonly [T, T]β
The array to iterate over.
iteratee: (value) => numberβ
A function that returns the value to compare.
Overload 2:
array: readonly T[]β
The array to iterate over.
iteratee: (value) => numberβ
A function that returns the value to compare.
Returns: Tβ
The element with the minimum value, or undefined if empty.
Sinceβ
2.0.0
Performanceβ
O(n) time, O(1) space.
Also known asβ
min (Radashi) Β· minBy (Lodash, es-toolkit, Remeda, Ramda) Β· β (Effect, Modern Dash, Antfu)
Exampleβ
minBy([{ name: 'John', age: 25 }, { name: 'Bob', age: 20 }] as const, (u) => u.age);
// => { name: 'Bob', age: 20 }
minBy([{ name: 'John', age: 25 }, { name: 'Bob', age: 20 }], (u) => u.age);
// => { name: 'Bob', age: 20 }
minBy([], (x) => x);
// => undefined
How it works?β
Finds the element with the minimum value computed by iteratee.
Use Casesβ
Find the cheapest product πβ
Identify the lowest-priced item for discounts or budget analysis. Perfect for e-commerce, price comparison, or deal finders.
const cartItems = [
{ name: "Headphones", price: 149.99 },
{ name: "USB Cable", price: 12.99 },
{ name: "Mouse Pad", price: 9.99 },
];
const cheapest = minBy(cartItems, (item) => item.price);
// => { name: "Mouse Pad", price: 9.99 }
Get the nearest locationβ
Find the closest point by distance calculation. Ideal for location services, delivery apps, or proximity searches.
const userPos = { lat: 48.8566, lng: 2.3522 };
const stores = [
{ name: "Store A", lat: 48.8698, lng: 2.3076 },
{ name: "Store B", lat: 48.8566, lng: 2.3629 },
{ name: "Store C", lat: 48.8867, lng: 2.3431 },
];
const nearest = minBy(stores, (s) =>
Math.hypot(s.lat - userPos.lat, s.lng - userPos.lng)
);
// => { name: "Store B", ... }
Find the oldest pending taskβ
Identify tasks waiting the longest to prevent queue starvation. Useful for task management, ticketing, or job schedulers.
const tasks = [
{ id: "T1", title: "Fix bug", createdAt: new Date("2024-01-15") },
{ id: "T2", title: "Update docs", createdAt: new Date("2024-01-18") },
{ id: "T3", title: "Add feature", createdAt: new Date("2024-01-10") },
];
const oldest = minBy(tasks, (t) => t.createdAt.getTime());
// => { id: "T3", title: "Add feature", createdAt: Date("2024-01-10") }
Find the lightest asset for performance optimizationβ
Identify the smallest file in a bundle for lazy-loading priority. Essential for performance audits and asset optimization pipelines.
const assets = [
{ name: "vendor.js", size: 245000 },
{ name: "app.js", size: 89000 },
{ name: "styles.css", size: 12000 },
{ name: "icons.svg", size: 3400 },
];
const smallest = minBy(assets, (a) => a.size);
// => { name: "icons.svg", size: 3400 }
Find the lowest-scoring page in an SEO auditβ
Identify the worst-performing page for prioritized SEO fixes. Critical for SEO dashboards and site audit tools.
const pages = [
{ url: "/home", seoScore: 92 },
{ url: "/about", seoScore: 78 },
{ url: "/products", seoScore: 55 },
{ url: "/blog", seoScore: 88 },
];
const worstPage = minBy(pages, (p) => p.seoScore);
// => { url: "/products", seoScore: 55 }
console.log(`Priority fix: ${worstPage?.url}`);