一、数组方法简单总结为以下几种
1、原有: 增、删、改、截、拼、复、排、转
2、ES5扩展: 查、遍历
增:
前增 var arr = [3,4,5,6,7]; console.log(arr.unshift(1,2,[true,false])); //8 console.log(arr); //Array(8) [ 1, 2, (2) […], 3, 4, 5, 6, 7 ]
后增 var arr = [3,4,5,6,7]; console.log(arr.push(1,2,[true,false])); //8 console.log(arr); //Array(8) [ 3, 4, 5, 6, 7, 1, 2, (2) […] ]
删:
前删 var arr = [3,4,5,6,7]; //3 删除的元素 console.log(arr.shift()); //Array(4) [ 4, 5, 6, 7 ] console.log(arr); 后删 var arr = [3,4,5,6,7]; console.log(arr); console.log(arr.pop()); //7 console.log(arr); //Array(4) [ 3, 4, 5, 6 ]
改:
改 var arr = [3,4,5,6,7]; // console.log(arr); console.log(arr.splice(1,2)); //从下标1开始,删除2个元素 //Array [ 4, 5 ] console.log(arr); //Array(3) [ 3, 6, 7 ] var arr = [3,4,5,6,7]; console.log(arr); console.log(arr.splice(1,2,8,9,[true,false,null])); //Array [ 4, 5 ] //把4,5改成8,9,[true,false,null] console.log(arr); //Array(6) [ 3, 8, 9, (3) […], 6, 7 ] var arr = [3,4,5,6,7]; console.log(arr); console.log(arr.splice(1,2,8,9,[true,false,null])); //Array [ 4, 5 ] console.log(arr); //Array(6) [ 3, 8, 9, (3) […], 6, 7 ] var arr = [3,4,5,6,7]; console.log(arr); console.log(arr.splice(1,0,8,9,[true,false,null])); //Array [] console.log(arr); //Array(8) [ 3, 8, 9, (3) […], 4, 5, 6, 7 ]
截: