@stdlib/stats-array-min-by
v0.1.1
Published
Calculate the minimum value of an array via a callback function.
Readme
minBy
Calculate the minimum value of an array via a callback function.
Installation
npm install @stdlib/stats-array-min-byUsage
var minBy = require( '@stdlib/stats-array-min-by' );minBy( x, clbk[, thisArg] )
Computes the minimum value of an array via a callback function.
function accessor( v ) {
return v * 2.0;
}
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
var v = minBy( x, accessor );
// returns -10.0The function has the following parameters:
- x: input array.
- clbk: callback function.
- thisArg: execution context (optional).
The invoked callback is provided three arguments:
- value: current array element.
- index: current array index.
- array: input array.
To set the callback execution context, provide a thisArg.
function accessor( v ) {
this.count += 1;
return v * 2.0;
}
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
var context = {
'count': 0
};
var v = minBy( x, accessor, context );
// returns -10.0
var cnt = context.count;
// returns 8Notes
- If provided an empty array, the function returns
NaN. - A provided callback function should return a numeric value.
- If a provided callback function does not return any value (or equivalently, explicitly returns
undefined), the value is ignored. - The function supports array-like objects having getter and setter accessors for array element access (e.g.,
@stdlib/array-base/accessor).
Examples
var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var minBy = require( '@stdlib/stats-array-min-by' );
function accessor( v ) {
return v * 2.0;
}
var x = discreteUniform( 10, -50, 50, {
'dtype': 'float64'
});
console.log( x );
var v = minBy( x, accessor );
console.log( v );Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2026. The Stdlib Authors.
