1. 合并两个数组:
var a = [1,2,3]; var b = [4,5,6]; Array.prototype.push.apply(a, b);
// a = [1, 2, 3, 4, 5, 6]
2. 取数组最大值和最小值:
Math.max.apply(Math, [1,2,3]) //3 Math.min.apply(Math, [1,2,3]) //1
3. 将一个数组插入另一个数组的指定位置:
var a = [1,2,3,7,8,9]; var b = [4,5,6]; var insertIndex = 3; [].splice.apply(a, Array.concat(insertIndex, 0, b));
// a = [1,2,3,4,5,6,7,8,9]
4. 交换数组:
var a = [1, 2, 3]; var b = [4, 5, 6]; a= [b, b=a][0]; // a = [4, 5, 6]; // b = [1, 2, 3];
5. 删除数组元素:
var a = [1,2,3,4,5]; a.splice(3,1); // a = [1, 2, 3, 5]
... to be continued.