理顺 JavaScript (11) - 数组
2010-09-14 13:40:47 来源:WEB开发网var arr;
arr = [1, 2, 3]
arr.unshift(5, 6);
alert(arr.join()); //5,6,1,2,3
arr.shift();
arr.shift();
alert(arr.join()); //1,2,3
arr.unshift([5,6,7,8]);
alert(arr.join()); //5,6,7,8,1,2,3
arr.shift();
alert(arr.join()); //1,2,3
splice : 删除、替换、插入; 如果有删除则返回被删除元素的数组
var arr;
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr.splice(4, 3); //从第 4 个(0起)开始删除 3 个
alert(arr.join()); //1, 2, 3, 4, 8
arr.splice(3, 3, 'A', 'B', 'C'); //从第 3 个(0起)开始替换 3 个
alert(arr.join()); //1, 2, 3, A, B, C
arr.splice(3, 0, 7, 8, 9); //从第 3 个(0起)开始插入 3 个; 插入时第二个参数给 0
alert(arr.join()); //1, 2, 3, 7, 8, 9, A, B, C
slice : 提取子数组; 返回新数组
var arr;
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr = arr.slice(2, 5);
alert(arr.join()); //3,4,5
/* 参数为负表示从后面数 */
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr = arr.slice(2, -2);
alert(arr.join()); //3,4,5,6
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr = arr.slice(-3, -1);
alert(arr.join()); //6,7
Tags:理顺 JavaScript 数组
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接