Skip to content

compute leq

kgryte edited this page May 12, 2015 · 1 revision

Computes an element-wise comparison (less than or equal to) for each input array element. x may either be an array of equal length or a single value (number or string).

The function returns an array with a length equal to that of the input array. Each output array element is either 0 or 1. A value of 1 means that an element is less than or equal to a compared value and 0 means that an element is not less than or equal to a compared value.

var data = [ 2, 4, 2, 7, 3 ],
	out;

out = compute.leq( data, 3.14 );
// returns [ 1, 0, 1, 0, 1 ]

out = compute.leq( data, [3, 5, 1, 7, 4 ] );
// returns [ 1, 1, 0, 1, 1 ]

For object arrays, provide an accessor function for accessing array values.

var data = [
	['beep', 5],
	['boop', 3],
	['bip', 8],
	['bap', 3],
	['baz', 2]
];

function getValue( d, i ) {
	return d[ 1 ];
}

var out = compute.leq( data, 3, {
	'accessor': getValue
});
// returns [ 0, 1, 0, 1, 1 ]

When comparing values between two object arrays, provide an accessor function which accepts 3 arguments.

var data = [
	['beep', 5],
	['boop', 3],
	['bip', 8],
	['bap', 3],
	['baz', 2]
];

var arr = [
	{'x': 4},
	{'x': 3},
	{'x': 6},
	{'x': 5},
	{'x': 3}
];

function getValue( d, i, j ) {
	if ( j === 0 ) {
		return d[ 1 ];
	}
	return d.x;
}

var out = compute.leq( data, arr, {
	'accessor': getValue
});
// returns [ 0, 1, 0, 1, 1 ]

For additional options, see compute-leq.

Clone this wiki locally