Skip to content

compute gt

kgryte edited this page May 12, 2015 · 1 revision

Computes an element-wise comparison (greater than) 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 greater than a compared value and 0 means that an element is not greater than a compared value.

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

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

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

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.gt( data, 4, {
	'accessor': getValue
});
// returns [ 1, 0, 1, 0, 0 ]

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': 5},
	{'x': 6},
	{'x': 5},
	{'x': 3}
];

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

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

For additional options, see compute-gt.

Clone this wiki locally