您的位置:首页 > Web前端 > JavaScript

JavaScript Array 数组操作使用大全

2014-03-30 14:15 656 查看
定义数组方式:

var array = new Array();
var array = Array();
var array = ["one", "two", "three"];
var array = [];

数组检测:

value instanceof Array
Array.isArray(value)  //IE9+,Firefox 4+,Safari 5+ ,Opera 10.5+,Chrome

转换方式:

var array = ["one", "two", "three"];
array.toString();
array.valueOf();
array.join(',');//随便一个分隔字符
array.toLocaleString(); //最好不要使用此方法,转换结果有可能根据代码或者浏览器版本返回不一样的结果

栈方法:

push向数组末尾压入一项,pop向数组末尾弹出一项;

var array = ["two", "three"];
array.push("one"); //array = ["two","three","one"]
var item = array.pop(); //array=["two","three"];item="one";

队列方式:

unshift向数组前端压入,shift向数组前端弹出

var array = ["one","two", "three"];
var item = array.shift(); //array = ["two","three"]; item = "one";
array.unshift("one"); //array=["one","two","three"];

排序方法:

var array = new Array();
array.sort();//正向排序
array.reverse(); //反向排序
array.sort(fn);//fn返回0,-1,1
示例:
function compare(value1, value2) {
if (value1 < value2) {
return -1;
} else if (value1 > value2) {
return 1;
} else {
return 0;
}
}

var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values);    //0,1,5,10,15

操作方法:

concat 用法

var colors = ["red", "green", "blue"];
var colors2 = colors.concat("yellow", ["black", "brown"]);

alert(colors);     //red,green,blue
alert(colors2);    //red,green,blue,yellow,black,brown

slice 用法

var colors = ["red", "green", "blue", "yellow", "purple"];
var colors2 = colors.slice(1);
var colors3 = colors.slice(1,4);

alert(colors2);   //green,blue,yellow,purple
alert(colors3);   //green,blue,yellow

splice 用法

var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1);              //remove the first item
alert(colors);     //green,blue
alert(removed);    //red - one item array

removed = colors.splice(1, 0, "yellow", "orange");  //insert two items at position 1
alert(colors);     //green,yellow,orange,blue
alert(removed);    //empty array

removed = colors.splice(1, 1, "red", "purple");    //insert two values, remove one
alert(colors);     //green,red,purple,orange,blue
alert(removed);    //yellow - one item array

数组搜索

//IE9+,Firefox 4+,Safari 5+ ,Opera 10.5+,Chrome
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4));        //3
alert(numbers.lastIndexOf(4));    //5
alert(numbers.indexOf(4, 4));     //5
alert(numbers.lastIndexOf(4, 4)); //3
var person = { name: "Nicholas" };
var people = [{ name: "Nicholas" }];
var morePeople = [person];
alert(people.indexOf(person));     //-1
alert(morePeople.indexOf(person)); //0

迭代方式

//IE9+,Firefox 4+,Safari 5+ ,Opera 10.5+,Chrome  ; every(),filter(),forEach(),map(),some();

var numbers = [1,2,3,4,5,4,3,2,1];

var everyResult = numbers.every(function(item, index, array){
return (item > 2);
});

alert(everyResult);       //false

var someResult = numbers.some(function(item, index, array){
return (item > 2);
});

alert(someResult);       //true

var filterResult = numbers.filter(function(item, index, array){
return (item > 2);
});

alert(filterResult);   //[3,4,5,4,3]

var mapResult = numbers.map(function(item, index, array){
return item * 2;
});

alert(mapResult);   //[2,4,6,8,10,8,6,4,2]

归并方法:

var values = [1,2,3,4,5];
var sum = values.reduce(function(prev, cur, index, array){
return prev + cur;
});
alert(sum);

var sum = values.reduceRight(function(prev, cur, index, array){
return prev + cur;
});
alert(sum);

函数call与apply的区别:
call要指定参数逐个列举的构造,apply只需要指定两个参数
funcation sum(num1,num2){
return sum.call(this,num1,num2);
}
funcation sum(num1,num2){
return sum.apply(this,arguments);//return sum.apply(this,[num1,num2]);
}
递归防止方法名改变的方法:

function factorial(num1){

if(num<1){

return 1;

}else{

return arguments.callee.caller(num1-1);

}

}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: