shift()、unshift()函数
var color=new Array();
color.push("red","green","yellow");
console.log(color);
var red=color.shift();//从数组头部开始移除数据,和push组合能模拟队列结构
console.log(red);
console.log(color);
color.unshift("red");//在数组头部强行插入数据和pop组合也能模拟另一个角度的队列结构
console.log(color);
color.pop();
console.log(color);
sort()函数
var values=[1,10,5,20,15];
values.sort();
console.log(values);
function compare(v1,v2){
if(v1>v2){
return 1;
}else if(v1<v2){
return -1;
}else{
return 0;
}
}
values.sort(compare);
console.log(values);//sort()函数返回的都是比较完了之后的数组
concat()方法
// concat()方法,先创建当前数组一个副本,然后将接收的参数添加到这个副本的末尾,最后返回新构建的数组
var colors=["red","green","blue"];
var colors1=colors.concat("yellow",["black","brown"]);
console.log(colors);//["red","green","blue"]
console.log(colors1);
slice()它能够基于当前数组中的一或多个创建一个新数组。slice()可以接受一个或者两个参数,即要返回项的起始或者结束位置。注意:slice不会影响原数组
splice()方法
1. 删除:可以删除任意数量的项,只需指定两个参数:要删除的第一项的位置和要删除的项数。
2. 插入:可以向指定的位置插入任意数量的项,只需提供3个参数:起始位置、0(要删除的项数)和要插入的项
3. 替换:可以向指定位置插入任意数量的项,同时删除任意数量的项。
splice()方法始终都返回一个数组,该数组中包含从原始数组中删除的项。
var colors=["red","green","blue"];
var removed=colors.splice(0,1);
console.log(removed);//red
console.log(colors);//green,blue
colors.splice(1,0,"yellow","orange");
console.log(colors);//green yellow blue orange
indexOf()和lastIndexOf,这两个方法都结收两个参数:要查找的项和表示查找起点位置的索引。indexOf()表示从数组的开头开始向后查找,lastIndexOf()表示从数组的末尾开始向前查找。
color.indexof("red",2);//-1
迭代方法
1.every()
2.filter()
3.forEach()
4.map()
5.some()
//eg
var number=[1,2,3,4,20,32,22];
var obj={
number:[1,23,32,42,21],
name:"wky",
age:"21"
}
var everyResult=obj.number.every(function (value,index,array) {
console.log(value);
return value>0;
});
var filterResult=obj.number.filter(function(value,index,array){
return value>10;
});
var mapResult=obj.number.map(function(value ,index,array){
return value*2;
});
console.log(everyResult);//true
console.log(filterResult);//[23,32,42,21]
console.log(mapResult);//[2,46,84,42]
var forResult=obj.number.forEach(function(value,index,array){
console.log(value);
});
reduce() reduceRight()归并函数 接收参数:前一个值,当前值,项的索引和数组对象