javascriptの配列Aray
5900 ワード
var a=new Array();
console.log(a);
a=new Array(3);
console.log(a);// 3
a=new Array("abc","xyz");
console.log(a);
// ,
a=Array(3);
a=Array("abc","xyz");
//
a=[];
a=[3];
a=["abc","xyz"];
console.log(a.length);//
console.log(a[1]);//
a.length=1;//
console.log(a[1]);//undefined
a[a.length]="asd";// asd
console.log(a);//
a[a.length]="zxc";//
console.log(a);//
a=["0","1","2"];
a[99]=99;// 100 99
console.log(a.length);// 100
console.log(a[4]);//undefined,
a=["qwe","asd","zxc"];
console.log(a instanceof Array);// , BUG
console.log(Array.isArray(a));// , ,IE8
console.log(a.toString());//qwe,asd,zxc
console.log(a.valueOf());//["qwe","asd","zxc"]
console.log(a);//["qwe","asd","zxc"]
//alert(a.toString);
//alert(a.valueOf());
//alert(a);
console.log(a.join("||"));//qwe||asd||zxc,
a.length=5;
console.log(a.join("||"));//qwe||asd||zxc||||
console.log(a.toString());//qwe,asd,zxc,,
var a=[];
var length=a.push("wer","sdf");//
console.log(length);//
console.log(a);//
length=a.push("w");//
console.log(length);//
console.log(a);//
var item=a.pop();//
console.log(item);//
console.log(a);//
item=a.pop();//
console.log(item);//
console.log(a);//
a=["z","x","c"];
item=a.shift();//
console.log(item);//
console.log(a);//
length=a.unshift("as");//
console.log(length);//
console.log(a);//
a=[1,2,3,4,5];
console.log(a.reverse());// ,
a=[1,2,3,4,10,5];
console.log(a.reverse());//5,10,4,3,2,1 ,
console.log(a.sort());//1,10,2,3,4,5 ,sort ,10 2
//sort , java
function compare(a,b){
if(ab)return -1;
else return 0;
}
console.log(a.sort(compare));//10,5,4,3,2,1
a=[1,2];
console.log(a.concat(5,6,[3,4]));//[1,2,5,6,3,4]
a=[1,2,3,4,5,6];
console.log(a.slice(3));//[4,5,6]
console.log(a.slice(3,5));//[3,4]
console.log(a.slice(5,3));//[]
console.log(a.slice(7));//[]
a=[1,2,3,4,5,6,7];
aa=a.splice(1,2);// ,
console.log(a);//[1,4,5,6,7] a
console.log(aa);//[2,3]
a=[1,2,3,4,5,6,7];
aa=a.splice(1,0,"7","8","9");// ,
console.log(a);//[1,"7","8","9",2,3,4,5,6,7] a
console.log(aa);//[]
a=[1,2,3,4,5,6,7];
aa=a.splice(1,1,"7","8","9");// ,
console.log(a);//[1,"7","8","9",3,4,5,6,7] a, 2
console.log(aa);//2 ,
a=[1,2,3,4,5,4,3,2,1];
console.log(a.indexOf(2));//1
console.log(a.indexOf(2,3));//7 2,
console.log(a.lastIndexOf(2));//7 ,
console.log(a.lastIndexOf(2,3));//1
console.log(a.lastIndexOf(9));//-1 1
console.log(a.lastIndexOf("2"));//-1 ===
IE 8および以下はindexOfをサポートしていません.次のようにサポートしてもいいです.if (!Array.prototype.indexOf){ //
Array.prototype.indexOf = function(elt /*, from*/){
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
a=[1,2,3,4,5];
aa=a.every(function(item,index,array){
console.log(item);//
console.log(index);//
console.log(array);//
return item>=1;// 1 true
})
console.log(aa);//true
aa=a.every(function(item,index,array){
console.log(item);//
console.log(index);//
console.log(array);//
return item>2;// 2,
})
console.log(aa);//false
a=[1,2,3,4,5];
aa=a.some(function(item,index,array){
return item>4;// true
})
console.log(aa);//true
aa=a.every(function(item,index,array){
return item>6;//
})
console.log(aa);//false
a=[1,2,3,4,5];
aa=a.filter(function(item,index,array){
return item>3;//
})
console.log(aa);//[4,5]
a=[1,2,3,4,5];
aa=a.map(function(item,index,array){
return item*3;
})
console.log(aa);//[3,6,9,12,15]
aa=a.map(function(item,index,array){
return item>3;
})
console.log(aa);//[false,false,false,true,true]
a=[1,2,3,4,5];
a.forEach(function(item,index,array){
console.log(item);
console.log(index);
console.log(array);
})
a=[1,2,3,4,5];
aa=a.reduce(function(pre,cur,index,array){
console.log(pre+" "+cur+" "+index);pre return ,cur
return pre+cur;
})
console.log(aa);//15
// reduceRight ,