max_by

statistics

Find maximum by a key function

Syntax

max_by(array, key)

Parameters

array (array)

The data to search

key (expression)

The key to compare by

Returns

object

The item with the maximum value for the key

Examples

Product with maximum price
Input:
max_by(.price)
Output:
{"name": "Premium Laptop", "price": 1999.99}
Student with highest score
Input:
max_by(.score)
Output:
{"name": "Sarah", "score": 98}

The max_by() function finds the item in an array that has the maximum value for a specified key or expression.

Usage

Use max_by() to find the entire record/object that has the largest value for a particular field, rather than just the maximum value itself.

Related Functions