Skip to content

compute cprod

kgryte edited this page May 12, 2015 · 1 revision

Computes the cumulative product of an array.

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

var arr = compute.cprod( data );
// returns [ 2, 8, 16, 112, 336 ]

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

var data = [
	[1,2],
	[2,1],
	[3,3]
];

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

var arr = compute.cprod( data, {
	'accessor': getValue
});
// returns [ 2, 2, 6 ]

By default, the method returns a new array. To calculate the cumulative product in-place, set the copy option to false to mutate the input array.

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

var arr = compute.cprod( data, {
	'copy': false
});
// returns [ 2, 8, 16, 112, 336 ]

console.log( data === arr );
// returns true
Clone this wiki locally