사용 复主代码 代码如下: <br>var isNumeric = function(x) {<br> // x가 숫자이면 true를 반환하고, 숫자가 아니면 false를 반환합니다.<br> var RegExp = /^(-)?(d*)(.?)(d*)$/ ; <br> return String(x).match(RegExp);<br>}<br>var myArray = [1,'two',3,'four',5,'six',7,'eight',9 ,'ten'];<br>varododArray=myArray.filter(isNumeric); // 출력: 1,3,5,7,9<br>varoddArray=myArray.some(isNumeric); // 출력: true<br>varododArray=myArray.every(isNumeric); // 출력: false<br>var printArray =function(x, idx){<br> document.writeln('[' idx '] = ' x);<br>}<br>myArray.forEach(printArray); // 출력: [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5<br>myArray.remove(9);<br>document.writeln(myArray); <br> </div> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="38590" class="copybut" id="copybut38590" onclick="doCopy('code38590')"><u>复主代码</u></a></span> 代码如下:</div> <div class="codebody" id="code38590"> <br>if (!Array.prototype.every) <br>{<br> Array.prototype.every = function(fun /*, thisp*/)<br> {<br> var len = this.length;<br> if (typeof fun != "function")<br> throw new TypeError(); <p> var thisp = arguments[1];<br> for (var i = 0; i < len; i++)<BR> {<BR> if (i in this &&<BR> !fun.call(thisp, this[i], i, this))<BR> return false;<BR> }</P> <P> return true;<BR> };<BR>}<BR>if (!Array.prototype.filter)<BR>{<BR> Array.prototype.filter = function(fun /*, thisp*/)<BR> {<BR> var len = this.length;<BR> if (typeof fun != "function")<BR> throw new TypeError();</P> <P> var res = new Array();<BR> var thisp = arguments[1];<BR> for (var i = 0; i < len; i++)<BR> {<BR> if (i in this)<BR> {<BR> var val = this[i]; // in case fun mutates this<BR> if (fun.call(thisp, val, i, this))<BR> res.push(val);<BR> }<BR> }</P> <P> return res;<BR> };<BR>}<BR>if (!Array.prototype.forEach)<BR>{<BR> Array.prototype.forEach = function(fun /*, thisp*/)<BR> {<BR> var len = this.length;<BR> if (typeof fun != "function")<BR> throw new TypeError();</P> <P> var thisp = arguments[1];<BR> for (var i = 0; i < len; i++)<BR> {<BR> if (i in this)<BR> fun.call(thisp, this[i], i, this);<BR> }<BR> };<BR>}<BR>if (!Array.prototype.map)<BR>{<BR> Array.prototype.map = function(fun /*, thisp*/)<BR> {<BR> var len = this.length;<BR> if (typeof fun != "function")<BR> throw new TypeError();</P> <P> var res = new Array(len);<BR> var thisp = arguments[1];<BR> for (var i = 0; i < len; i++)<BR> {<BR> if (i in this)<BR> res[i] = fun.call(thisp, this[i], i, this);<BR> }</P> <P> return res;<BR> };<BR>}<BR>if (!Array.prototype.some)<BR>{<BR> Array.prototype.some = function(fun /*, thisp*/)<BR> {<BR> var len = this.length;<BR> if (typeof fun != "function")<BR> throw new TypeError();</P> <P> var thisp = arguments[1];<BR> for (var i = 0; i < len; i++)<BR> {<BR> if (i in this &&<BR> fun.call(thisp, this[i], i, this))<BR> return true;<BR> }</P> <P> return false;<BR> };<BR>}<BR>Array.prototype.sortNum = function() {<BR> return this.sort( function (a,b) { return a-b; } );<BR>}<BR><!--<BR>var tmp = [5,9,12,18,56,1,10,42,'blue',30, 7,97,53,33,30,35,27,30,'35','Ball', 'bubble'];<BR>var thirty=tmp.find(30); // Returns 9, 14, 17<BR>var thirtyfive=tmp.find('35'); // Returns 18<BR>var thirtyfive=tmp.find(35); // Returns 15<BR>var haveBlue=tmp.find('blue'); // Returns 8<BR>var notFound=tmp.find('not there!'); // Returns false<BR>var regexp1=tmp.find(/^b/); // returns 8,20 (first letter starts with b)<BR>var regexp1=tmp.find(/^b/i); // returns 8,19,20 (same as above but ignore case)<BR>--><br>Array.prototype.find = function(searchStr) {<br> var returnArray = false;<br> for (i=0; i<this.length; i++) {<BR> if (typeof(searchStr) == 'function') {<BR> if (searchStr.test(this[i])) {<BR> if (!returnArray) { returnArray = [] }<BR> returnArray.push(i);<BR> }<BR> } else {<BR> if (this[i]===searchStr) {<BR> if (!returnArray) { returnArray = [] }<BR> returnArray.push(i);<BR> }<BR> }<BR> }<BR> return returnArray;<BR>}<BR>//随机改变数组的排序<BR>Array.prototype.shuffle = function (){ <BR> for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp); <BR> return this;<BR>} <BR><!--var myArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];<BR>var yourArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];<BR>document.writeln(myArray.compare(yourArray)); // outputs: true;--><br>Array.prototype.compare = function(testArr) {<br> if (this.length != testArr.length) return false;<br> for (var i = 0; i < testArr.length; i++) {<BR> if (this[i].compare) { <BR> if (!this[i].compare(testArr[i])) return false;<BR> }<BR> if (this[i] !== testArr[i]) return false;<BR> }<BR> return true;<BR>}<BR>//去掉数组中重复的值var a = new Array("5","7","7"); a.unique();<BR>Array.prototype.unique = function() {<BR> var data = this || [];<BR> var a = {}; //声明一个对象,javascript的对象可以当哈希表用<BR> for (var i = 0; i < data.length; i++) {<BR> a[data[i]] = true; //设置标记,把数组的值当下标,这样就可以去掉重复的值<BR> }<BR> data.length = 0; <br><br> for (var i in a) { //遍历对象,把已标记的还原成数组<BR> this[data.length] = i; <BR> } <BR> return data;<BR>}</P> <P>Array.prototype.addAll = function($array)<BR>{<BR> if($array == null || $array.length == 0)<BR> return;</P> <P> for(var $i=0; $i<$array.length; $i++)<BR> this.push($array[$i]);<BR>}</P> <P>Array.prototype.contains = function($value)<BR>{<BR> for(var $i=0; $i<this.length; $i++)<BR> {<BR> var $element = this[$i];<BR> if($element == $value)<BR> return true;<BR> }</P> <P> return false;<BR>}</P> <P>Array.prototype.indexOf = function($value)<BR>{<BR> for(var $i=0; $i<this.length; $i++)<BR> {<BR> if(this[$i] == $value)<BR> return $i;<BR> }</P> <P> return -1;<BR>}<BR>if (!Array.prototype.lastIndexOf)<BR>{<BR> Array.prototype.lastIndexOf = function(elt /*, from*/)<BR> {<BR> var len = this.length;</P> <P> var from = Number(arguments[1]);<BR> if (isNaN(from))<BR> {<BR> from = len - 1;<BR> }<BR> else<BR> {<BR> from = (from < 0)<BR> ? Math.ceil(from)<BR> : Math.floor(from);<BR> if (from < 0)<BR> from += len;<BR> else if (from >= len)<br> from = len - 1;<br> }</p> <p> for (; from > -1; from--)<br> {<br> if (from in this &&<br> this[from] === elt)<br> return from;<br> }<br> return -1;<br> };<br>}<br>Array.prototype.insertAt = function($value, $index)<br>{<br> if($index < 0)<BR> this.unshift($value);<BR> else if($index >= this.length)<br> this.push($value);<br> else<br> this.splice($index, 0, $value);<br>}<br>/** <br>* 根据数组的下标来删除元素 <br>*/ <br>Array.prototype.removeByIndex=function($n) { <br> if($n<0){ //如果n<0,则不进行任何操作。 <BR> return this; <BR> }else{ <BR> return this.slice(0,$n).concat(this.slice($n+1,this.length)); <BR> } <BR>}<BR>//依赖indexOf<BR>Array.prototype.remove = function($value)<BR>{<BR> var $index = this.indexOf($value);</P> <P> if($index != -1)<BR> this.splice($index, 1);<BR>}</P> <P>Array.prototype.removeAll = function()<BR>{<BR> while(this.length > 0)<br> this.pop();<br>}</p> <p>Array.prototype.replace = function($oldValue, $newValue)<br>{<br> for(var $i=0; $i<this.length; $i++)<BR> {<BR> if(this[$i] == $oldValue)<BR> {<BR> this[$i] = $newValue;<BR> return;<BR> }<BR> }<BR>}</P> <P>Array.prototype.swap = function($a, $b)<BR>{<BR> if($a == $b)<BR> return;</P> <P> var $tmp = this[$a];<BR> this[$a] = this[$b];<BR> this[$b] = $tmp;<BR>}<BR>Array.prototype.max = function() { <BR> return Math.max.apply({}, this); <BR>} <BR>Array.prototype.min = function() { <BR> return Math.min.apply({}, this); <BR>} <BR>Array.prototype.splice = function(start, delLen, item){<BR> var len =this.length;<BR> start = start<0?0:start>len?len:start?start:0;<br> delLen=delLen<0?0:delLen>len?len:delLen?delLen:len; <br><br> var arr =[],res=[];<br> var iarr=0,ires=0,i=0;<br><br> for(i=0;i<len;i++){<BR> if(i<start|| ires>=delLen) arr[iarr++]=this[i];<br> else {<br> res[ires++]=this[i];<br> if(item&&ires==delLen){<br> arr[iarr++]=item;<br> }<br> } <br> }<br> if(item&&ires<delLen) arr[iarr]=item; <br><br> for(var i=0;i<arr.length;i++){<BR> this[i]=arr[i];<BR> }<BR> this.length=arr.length;<BR> return res;<BR>}<BR>Array.prototype.shift = function(){ if(!this) return[];return this.splice(0,1)[0];}</P> <P>//分开添加,关键字shallow copy,如果遇到数组,复制数组中的元素<BR>Array.prototype.concat = function(){<BR> var i=0;<BR> while(i<arguments.length){<BR> if(typeof arguments[i] === 'object'&&typeof arguments[i].splice ==='function' &&!arguments[i].propertyIsEnumerable('length')){<BR> // NOT SHALLOW COPY BELOW<BR> // Array.prototype.concat.apply(this,arguments[i++]);<BR> var j=0;<BR> while(j<arguments[i].length) this.splice(this.length,0,arguments[i][j++]);<BR> i++;<BR> } else{<BR> this[this.length]=arguments[i++];<BR> }<BR> }<BR> return this;<BR>}</P> <P>Array.prototype.join = function(separator){<BR> var i=0,str="";<BR> while(i<this.length) str+=this[i++]+separator;<BR> return str;<BR>}</P> <P>Array.prototype.pop = function() { return this.splice(this.length-1,1)[0];}</P><P>Array.prototype.push = function(){ <BR> Array.prototype.splice.apply(this,<BR> [this.length,0].concat(Array.prototype.slice.apply(arguments)) ); //这里没有直接处理参数,而是复 제조사了一下<BR> return this.length;<BR>}<BR>Array.prototype.reverse = function(){<BR> for(var i=0;i< this.length/2;i ){<BR> var temp = this[i];<BR> this[i]= this[this.length-1-i];<BR> this[this.length-1- i] = temp;<BR> }<BR> return this;<BR>}<BR>Array.prototype.slice = function(start, end){<BR> var len =this.length;<BR> start= start<0?start =len:start?start:0;<BR> end =end<0?end =len:end>len?len:end?end:len;<br><br> var i=start; <br> var res = [];<br> while(i<end){<br> res.push(this[i ]);<br> }<br> return res; <br>}<br>//arr.unshift(ele1,ele2,ele3....)<br>Array.prototype.unshift =function(){<br> Array.prototype.splice.apply(this,[ 0,0].concat(Array.prototype.slice.apply(this,arguments)));<br>}<br></p> </div>