Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

plus.as

Name: plus() - adds 2 arrays by value;
Author: senocular: www.senocular.com
Date: 1899-12-31T00:25:43.700
Documentation:
Array PLUS - adds values of 2 arrays by index (ie. index 1 of
each array is added together followed by index 2, etc.) and returns
as a new array

Arguments:
a - the array to add to the current array.
trim - (optional) boolean determining whether or not the returned array is cut off to be the
length of the shorter array.  If trim is not passed or is set to false, the returned array
is the length of the larger array and those values have no operation imposed on them

Note:
- you can easily convert this proto to use any type of operation such as minus, divide, or
multiply (among others) ex:

Array.prototype.multiply = function(a, trim){
	var i=this.length, j=a.length, r=[];
	r = (trim ^ (i > j)) ? r = this.slice() : a.slice();
	i = Math.min(i,j);
	while(i--) r[i] = this[i] * a[i];
	return r;
}

 

Example:
a = [1,2,3,4,5,6];
b = [1,2,3,4];
trace(a.plus(b)); // 2,4,6,8,5,6
trace(a.plus(b, true)); // 2,4,6,8

1
2
3
4
5
6
7
Array.prototype.plus = function(a, trim){
	var i=this.length, j=a.length, r=[];
	r = (trim ^ (i > j)) ? r = this.slice() : a.slice();
	i = Math.min(i,j);
	while(i--) r[i] = this[i] + a[i];
	return r;
}