一、前言
push pop unshift shift splice slice split join 方法及使用区别
二、示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
const arr = [1, 2, 3, 4];
const a = arr.push('X', 'Y', 'Z');
const b = arr.unshift('A', 'B', 'C');
console.log(a);
console.log(b);
console.log(arr);
const c = arr.pop();
const d = arr.shift();
console.log(c);
console.log(d);
const e = arr.push();
const f = [].pop();
const g = [].shift();
console.log(e);
console.log(f);
console.log(g);
const h = arr.splice(-2, 2, 'rex', 'hang', 'gu');
console.log(h);
const i = arr.slice(1, -2);
console.log(i);
const j = 'a|b|c'.split('|');
console.log(j);
const k = ['a', 'b', 'c'].join('|');
console.log(k);
console.log(arr);
|
控制台输出内容如图所示:
看完上述例子后,我想大家都应该大致明白了这些方法的使用区别。