//-------------------------------------------------------------------------- //一个数字或者数组是否属于另外一个数组数据(roading.net) function inArr(a1, a2) { if (typeof (a1) == "object") { var k = 0; for (var i in a2) { for (var j in a1) { if (a2[i] == a1[j]) { k++; } } } return k == a1.length ? true : false; } else { for (var j in a2) { if (a1 == a2[j]) { return true; } } return false; } } // trace(inArr([1, 2, 8], [1, 2, 8])); trace(inArr([1, 2], [1, 2, 8])); trace(inArr(1, [1, 2, 8])); trace(inArr(1, {a:1, b:2, c:8})); // trace(inArr(["d1", "d2", "d8"], ["d1", "d2", "d8"])); trace(inArr(["d1", "d2"], ["d1", "d2", "d8"])); trace(inArr("d1", ["d1", "d2", "d8"])); trace(inArr("d1", {a:"d1", b:"d2", c:"d8"})); //-------------------------------------------------------------------------- // 数组中最大最小元素索引 Array.prototype.min = function() { var i = this.length; var min = this[0]; var idx = 0; while (i-->1) { if (this[i]1) { if (this[i]>max) { max = this[idx=i]; } } return idx; }; //usage a = [1, 5, 2, 6, 8]; idx = a.min(); trace("Found minimum "+a[idx]+" at "+idx); idx = a.max(); trace("Found maximum "+a[idx]+" at "+idx); //-------------------------------------------------------------------------- // 比较两个数组是否相等 Array.prototype.equals = function(a){ if (this == a) return true; var i = this.length; if (i != a.length) return false; while(i--) if (this[i] != a[i]) return false; return true; } //**** EXAMPLE **** a = [1,2,3,4]; b = [1,2,3,4]; c = [1,2,3,5]; d = a; trace(a.equals(b)); // true trace(a.equals(c)); // false trace(a.equals(d)); // true //-------------------------------------------------------------------------- // 数组中两个元素交换位置 // ? MSA // Version 1.3, 6.7.2002 - 6.11.2002 Array.prototype.exchange = function(i,j){ if (i != j) with (this[i]) { this[i] = this[j]; this[j] = valueOf(); } } //-------------------------------------------------------------------------- // 乱序整个数组 // ? MSA // Version 1.0, 4.10.2002 // Based on the Array.prototype.exchange Array.prototype.shuffle = function() { var i = this.length-1; if (i) do { this.exchange(i, Math.floor(Math.random()*(i+1))); } while (--i); } //**** EXAMPLE **** myArray = new Array (1,2,3,4,5,6,7,8,9,10); myArray.shuffle(); trace (myArray); //-------------------------------------------------------------------------- // 数组长度固定;向前,向后,当前位置三种方式逐个元素更新数据 Array.prototype._position = 0; Array.prototype.current = function(newVal){ if (!this.length) return undefined; if (!this._position) this._position = 0; else if (this._position >= this.length) this._position = this.length-1; if (newVal == undefined) return this[this._position]; var orig = this[this._position]; this[this._position] = newVal; return orig; }; Array.prototype.next = function(newVal){ if (!this.length) return undefined; if (this._position <= 0) this._position = 1; else if (this._position >= this.length-1) this._position = 0; else ++this._position; if (newVal == undefined) return this[this._position]; var orig = this[this._position]; this[this._position] = newVal; return orig; }; Array.prototype.previous = function(newVal){ if (!this.length) return undefined; if (this._position <= 0 || this._position >= this.length) this._position = this.length-1 else --this._position; if (newVal == undefined) return this[this._position]; var orig = this[this._position]; this[this._position] = newVal; return orig; }; //******** EXAMPLE ******** // cycle through an array with length 10 adding // the time which the mouse was clicked using // getTimer(). once the end is reached, the // _position is back to 0 and it repeats the // cycle starting with the 'front' of the array clickArray = new Array(10); clickArray.current(getTimer()); this.onMouseDown = function(){ clickArray.previous(getTimer()); trace("["+clickArray+"]"); } //-------------------------------------------------------------------------- // 判断一个值是否存在于数组当中,没有就在最后增加 // just verify an element is not already in before adding one. array.prototype.addDistinct = function(val){ for(var i in this) if(this[i] == val) return; return this.push(val); } //******** Example ******** var my_array = new Array("Tic", "Tac", "Toe"); my_array.addDistinct("Tac") // => doesn't push trace(my_array); my_array.addDistinct("Jim") // => all right trace(my_array); //-------------------------------------------------------------------------- // 整体偏移数组中元素位置 // Array.offset(): offsets and wraps the elements of an array n places. // swell code by reggossra Array.prototype.offset = function (offset) { if (offset < 0) { for (var i = 0; i < Math.abs(offset); i++) { // shift to get the first element, push it onto the end this.push(this.shift()); } } else { for (var i = 0; i < Math.abs(offset); i++) { // pop to get the last element, unshift it onto the front this.unshift(this.pop()); } } }; //******** Example ******** letters = ["a","b","c","d","e"]; trace (letters); // output: a,b,c,d,e letters.offset(-2); trace (letters); // output: c,d,e,a,b //-------------------------------------------------------------------------- // 数组中元素的平均数 Array.prototype.average = function() { var t = 0; for (var i in this) { if(typeof(this[i]) == "number") t+= this[i]; } return t/this.length; }; //******** Example ******** bob = [2, 2, 8, 8]; trace(bob.average()); //-------------------------------------------------------------------------- // 删除数组中某个元素 Array.prototype.remove = function(piIndex) { laTmpArray = new Array(); for (liIndex = 0; liIndex < this.length; liIndex ++) { if (liIndex != piIndex) laTmpArray[laTmpArray.length] = this[liIndex]; } return(laTmpArray); };