Array
- array.concat(item…)
var a=[1,2,3];
var b=[4,5,6];
var c=a.concat(b);
console.log(a); //(3) [1, 2, 3]
console.log(b); //(3) [4, 5, 6]
console.log(c); //(6) [1, 2, 3, 4, 5, 6]
- array.join(separator)
join方法是先把array中的每个元素构造成一个字符串,接着用一个separator分隔符连接在一起。
var a=['a','b','c'];
a.push('d');
var c=a.join('');
console.log(a); //(4) ["a", "b", "c", "d"]
console.log(c); //'abcd'
- array.pop()
var a=['a','b','c'];
var c=a.pop();
console.log(a); //(2) ["a", "b"]
console.log(c); // c
pop方法可以像这样实现:
Array.method('pop',function(){
return this.splice(this.length-1,1)[0];
});
- array.push(item…)
var a=['a','b','c'];
var b=['x','y','z'];
var c=a.push(b,true);
console.log(a); //(5) ["a", "b", "c", ['x','y','z'] , true]
console.log(b); //(3) ["x", "y", "z"]
console.log(c);//5
- array.reverse()
var a=['a','b','c'];
var b=a.reverse();
console.log(a); //(3) ["c", "b", "a"]
console.log(b); //(3) ["c", "b", "a"]
- array.shift()
var a=['a','b','c'];
var c=a.shift();
console.log(a); //(2) ["b", "c"]
console.log(c); //a
- array.slice(start,end)
如果两个参数中的任何一个是负数,array.length会和它们相加,让它们成为非负数。
var a=['a','b','c'];
var b=a.slice(0,1);
console.log(a); //(3) ["a", "b", "c"]
console.log(b); //["a"]
var c=a.slice(1);
console.log(a); //(3) ["a", "b", "c"]
console.log(c); //(2) ["b", "c"]
- array.sort(comparefn)
sort方法对array的内容进行排序,它不能正确的给一组数字排序。更多的关于排序的问题及拓展请移步:
http://blog.csdn.net/chengqunbin/article/details/79119251
var n=[4,5,8,35,43];
n.sort();
console.log(n);//(5) [35, 4, 43, 5, 8]
- array.splice(start,deleteCount,item…)
var a=['a','b','c'];
var r=a.splice(1,1,'ache','bug');
console.log(a); //(4) ["a", "ache", "bug", "c"]
console.log(r); //["b"]
- array.unshift(item..)
var a=['a','b','c'];
var r=a.unshift('?','$');
console.log(a); //(5) ["?", "$", "a", "b", "c"]
console.log(r); //5