Skip to content

compute msum

kgryte edited this page May 12, 2015 · 1 revision

Computes a moving sum over an array.

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

var values = compute.msum( data, 2 );
// returns [ 6, 6, 9, 10 ]

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

var data = [
	{'x':2},
	{'x':4},
	{'x':2},
	{'x':7},
	{'x':3}
];

function getValue( d ) {
	return d.x;
}

var values = compute.msum( data, 2, {
	'accessor': getValue	
});
// returns [ 6, 6, 9, 10 ]

By default, a new array is returned. To compute the sums in place, i.e., mutate the input array, set the copy option to false.

var data = [
	{'x':2},
	{'x':4},
	{'x':2},
	{'x':7},
	{'x':3}
];

function getValue( d ) {
	return d.x;
}

var values = compute.msum( data, 2, {
	'accessor': getValue,
	'copy': false	
});
// returns [ 6, 6, 9, 10 ]

console.log( values === copy );
// returns true
Clone this wiki locally