js中数组和字符串的方法总结

一、数组方法简单总结为以下几种

  1、原有:     增、删、改、截、拼、复、排、转

  2、ES5扩展:   查、遍历

增:

       前增
var arr = [,,,,];
console.log(arr.unshift(,,[true,false])); //
console.log(arr); //Array(8) [ 1, 2, (2) […], 3, 4, 5, 6, 7 ] 后增
var arr = [,,,,];
console.log(arr.push(,,[true,false])); //
console.log(arr); //Array(8) [ 3, 4, 5, 6, 7, 1, 2, (2) […] ]

删:

            前删
var arr = [,,,,]; //3 删除的元素
console.log(arr.shift()); //Array(4) [ 4, 5, 6, 7 ]
console.log(arr);
后删
var arr = [,,,,];
console.log(arr);
console.log(arr.pop()); //
console.log(arr); //Array(4) [ 3, 4, 5, 6 ]

改:


var arr = [,,,,]; //
console.log(arr);
console.log(arr.splice(,)); //从下标1开始,删除2个元素 //Array [ 4, 5 ]
console.log(arr); //Array(3) [ 3, 6, 7 ] var arr = [,,,,];
console.log(arr);
console.log(arr.splice(,,,,[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 = [,,,,];
console.log(arr);
console.log(arr.splice(,,,,[true,false,null])); //Array [ 4, 5 ]
console.log(arr); //Array(6) [ 3, 8, 9, (3) […], 6, 7 ] var arr = [,,,,];
console.log(arr);
console.log(arr.splice(,,,,[true,false,null])); //Array []
console.log(arr); //Array(8) [ 3, 8, 9, (3) […], 4, 5, 6, 7 ]

截:

上一篇:一起来Fit TDMA over WiFi(3)


下一篇:js中数组的字符串表示