1、push()
var ary1 = [1,2,3,4];
console.log(ary1.push(5,6,7));
console.log(ary1);
2、unshift()
var ary2 = [1,2,3,4];
console.log(ary2.unshift(5,6,7));
console.log(ary2);
3、pop()
var ary3 = [1,2,3,4];
console.log(ary3.pop());
console.log(ary3);
var ary33 = [];
console.log(ary33.pop());
console.log(ary33);
4、shift()
var ary4 = [1,2,3,4];
console.log(ary4.shift());
console.log(ary4);
var ary44 = [];
console.log(ary44.pop());
console.log(ary44);
5、concat()
var ary51 = [1,2,3,4];
var ary52 = ['a','b','c'];
var ary53 = ['甲','已','丙'];
var ary54 = [];
console.log(ary51.concat(ary52,ary53,ary54));
console.log(ary51);
console.log(ary52);
console.log(ary53);
6、join()
var ary61 = ['a','b','c'];
var ary62 = [111];
var ary63 = [1,2,3];
console.log(ary61.join());
console.log(ary62.join());
console.log(ary63.join('-'));
7、sort()
var ary71 = [2,3,1];
console.log(ary71.sort());
关于sort函数的使用,详见我的另一篇文章
8、reverse()
var ary8 = ['a','b','c'];
console.log(ary8.reverse());
9、forEach()
var ary91 = [1,2,3,4];
ary91.forEach(function(item){
item = item+ 1;
});
setTimeout(function(){
console.log(ary91);
},1000)
var ary93 = ['a','b','c'];
ary93.forEach(function(item){
item = item+ 'e';
});
setTimeout(function(){
console.log(ary93);
},1000)
var ary92 = [
{name:'甲', age: 18},
{name:'已', age: 19},
{name:'丙', age: 20}
]
ary92.forEach(function(item){
item.age +=1;
});
setTimeout(function(){
console.log(ary92);
},500)
10、map()
var kvArray = [
{key: 1, value: 10},
{key: 2, value: 20},
{key: 3, value: 30}];
var reformattedArray = kvArray.map(function(obj) {
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
});
11、filter()
var ary11 = [1,2,3,4];
var ary11New = ary11.filter(function(item){
if(item >= 3){
return item;
}
})
console.log(ary11New);
12、find()
var ary12 = [1,2,3,4];
var returnValue = ary12.find(function(item,index){
if(item >= 3){
var obj = {};
obj[index] = item;
return obj;
}
})
console.log(returnValue);
13、some()和every()
详见另一篇文章
14、splice()和slice()
详见另一篇文章