js array不支援map filter等的解決辦法

來源:互聯網
上載者:User

js array不支援map filter等的解決辦法


if (!Array.prototype.every)
{
Array.prototype.every = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
!fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
}
if (!Array.prototype.filter)
{
Array.prototype.filter = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
{
var val = this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}
if (!Array.prototype.some)
{
Array.prototype.some = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this &&
fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
}
Array.prototype.sortNum = function() {
return this.sort( function (a,b) { return a-b; } );
}

Array.prototype.find = function(searchStr) {
var returnArray = false;
for (i=0; i if (typeof(searchStr) == 'function') {
if (searchStr.test(this[i])) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
} else {
if (this[i]===searchStr) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
}
}
return returnArray;
}
//隨機改變數組的排序
Array.prototype.shuffle = function (){
for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
return this;
}

Array.prototype.compare = function(testArr) {
if (this.length != testArr.length) return false;
for (var i = 0; i < testArr.length; i++) {
if (this[i].compare) {
if (!this[i].compare(testArr[i])) return false;
}
if (this[i] !== testArr[i]) return false;
}
return true;
}
//去掉數組中重複的值var a = new Array("5","7","7"); a.unique();
Array.prototype.unique = function() {
var data = this || [];
var a = {}; //聲明一個對象,javascript的對象可以當雜湊表用
for (var i = 0; i < data.length; i++) {
a[data[i]] = true; //設定標記,把數組的值當下標,這樣就可以去掉重複的值
}
data.length = 0;

for (var i in a) { //遍曆對象,把已標記的還原成數組
this[data.length] = i;
}
return data;
}
Array.prototype.addAll = function($array)
{
if($array == null || $array.length == 0)
return;
for(var $i=0; $i<$array.length; $i++)
this.push($array[$i]);
}
Array.prototype.contains = function($value)
{
for(var $i=0; $i {
var $element = this[$i];
if($element == $value)
return true;
}
return false;
}
Array.prototype.indexOf = function($value)
{
for(var $i=0; $i {
if(this[$i] == $value)
return $i;
}
return -1;
}
if (!Array.prototype.lastIndexOf)
{
Array.prototype.lastIndexOf = function(elt /*, from*/)
{
var len = this.length;
var from = Number(arguments[1]);
if (isNaN(from))
{
from = len - 1;
}
else
{
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
else if (from >= len)
from = len - 1;
}
for (; from > -1; from--)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
Array.prototype.insertAt = function($value, $index)
{
if($index < 0)
this.unshift($value);
else if($index >= this.length)
this.push($value);
else
this.splice($index, 0, $value);
}
/**
* 根據數組的下標來刪除元素
*/
Array.prototype.removeByIndex=function($n) { 
if($n<0){ //如果n<0,則不進行任何操作。
  return this;
}else{
return this.slice(0,$n).concat(this.slice($n+1,this.length));
}
}
//依賴indexOf
Array.prototype.remove = function($value)
{
var $index = this.indexOf($value);
if($index != -1)
this.splice($index, 1);
}
Array.prototype.removeAll = function()
{
while(this.length > 0)
this.pop();
}
Array.prototype.replace = function($oldValue, $newValue)
{
for(var $i=0; $i {
if(this[$i] == $oldValue)
{
this[$i] = $newValue;
return;
}
}
}
Array.prototype.swap = function($a, $b)
{
if($a == $b)
return;
var $tmp = this[$a];
this[$a] = this[$b];
this[$b] = $tmp;
}
Array.prototype.max = function() {
return Math.max.apply({}, this);
}
Array.prototype.min = function() {
return Math.min.apply({}, this);
}
Array.prototype.splice = function(start, delLen, item){
var len =this.length;
start = start<0?0:start>len?len:start?start:0;
delLen=delLen<0?0:delLen>len?len:delLen?delLen:len;

var arr =[],res=[];
var iarr=0,ires=0,i=0;

for(i=0;i if(i=delLen) arr[iarr++]=this[i];
else {
res[ires++]=this[i];
if(item&&ires==delLen){
arr[iarr++]=item;
}
}
}
if(item&&ires

for(var i=0;i this[i]=arr[i];
}
this.length=arr.length;
return res;
}
Array.prototype.shift = function(){ if(!this) return[];return this.splice(0,1)[0];}
//分開添加,關鍵字shallow copy,如果遇到數組,複製數組中的元素
Array.prototype.concat = function(){
var i=0;
while(i if(typeof arguments[i] === 'object'&&typeof arguments[i].splice ==='function' &&!arguments[i].propertyIsEnumerable('length')){
// NOT SHALLOW COPY BELOW
// Array.prototype.concat.apply(this,arguments[i++]);
var j=0;
while(j i++;
} else{
this[this.length]=arguments[i++];
}
}
return this;
}
Array.prototype.join = function(separator){
var i=0,str="";
while(i return str;
}
Array.prototype.pop = function() { return this.splice(this.length-1,1)[0];}
Array.prototype.push = function(){
Array.prototype.splice.apply(this,
[this.length,0].concat(Array.prototype.slice.apply(arguments))); //這裡沒有直接處理參數,而是複製了一下
return this.length;
}
Array.prototype.reverse = function(){
for(var i=0;i var temp = this[i];
this[i]= this[this.length-1-i];
this[this.length-1-i] = temp;
}
return this;
}
Array.prototype.slice = function(start, end){
var len =this.length;
start=start<0?start+=len:start?start:0;
end =end<0?end+=len:end>len?len:end?end:len;

var i=start;
var res = [];
while(i res.push(this[i++]);
}
return res;
}
//arr.unshift(ele1,ele2,ele3....)
Array.prototype.unshift =function(){
Array.prototype.splice.apply(this,[0,0].concat(Array.prototype.slice.apply(this,arguments)));
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.