js 数组操作

合并数组

var a = [1,2,3];
var b = [4,5,6];
//  a + b ?
a + b == "1,2,34,5,6"
//a.push(b) ?
a.push(b);
a == [1, 2, 3, Array[3]]
//正确的做法
Array.prototype.push.apply(a,b);
a == [1,2,3,4,5,6];

删除指定索引

//删除 索引为 i 的元素
a.splice(2,1);
a == [1,2,4,5,6]

指定位置插入元素

a.splice(3,0,9);
a == [1,2,3,9,4,5,6]

你可能感兴趣的:(js 数组操作)