有趣的 javascript 数组
1. 关于数组的一些认识
- 使用构造器函数创建数组时:
- 若参数只有一个,且为数字,
let ary1 = new Array(3)
,这是其实我们是在指定数组的长度的,即 ary1.length === 3
。ary1[0]
则为undefined
;
- 当定义
let ary2 = new Array(1, 2, 3)
时,此时ary2 === [1, 2, 3]
;
- JS中的数组定义非常的自由:
看下面的例子:1 2 3 4
| let ary = [1, 2, 3]; ary[5] = 5; console.log(ary); console.log(ary.length);
|
再看:1 2 3
| let ary2 = [1, 2, 3, 4, 5]; ary2.length = 3; console.log(ary2);
|
2. 常用的数组API
- 万能方法
splice()
- splice()方法允许我们对数组进行插入、替换和删除的功能。splice方法返回一个有删除元素组成的新数组,没有删除时则返回一个空数组,简直完美呀!
splice()
方法接受三个参数,第一个为开始索引,第二个为删除元素的位置,第三个为插入的元素,可以为第二个,当然也可省略(表示删除元素)。
splice()
方法会修改原数组!
- 通过以下三个demo了解一下具体的用法吧:
插入: 1 2 3 4
| let ary1 = ["first", "second", "third", "forth", "fifth"]; let ary2 = ary1.splice(1,0,"add1"); console.log(ary1); console.log(ary2);
|
替换: 1 2 3 4
| let ary1 = ["first", "second", "third", "forth", "fifth"]; let ary2 = ary1.splice(1,1,"replace"); console.log(ary1); console.log(ary2);
|
删除: 1 2 3 4
| let ary1 = ["first", "second", "third", "forth", "fifth"]; let ary2 = ary1.splice(1,3); console.log(ary1); console.log(ary2);
|
slice()
方法
- 可以接受两个参数(start, end);
- 可从已有的数组中返回选定的元素;
- 可提取字符串的某个部分,并以新的字符串返回被提取的部分;
- 不会改变原始数组,而是返回一个新数组。
demo1:1 2 3 4 5
| let ary = ["first", "second", "third", "forth", "fifth"]; console.log (ary.slice(1,2) ); let ary1 = ary.slice(1,2); let ary2 = ary.slice(1,3); console.log(ary);
|
demo2:1 2 3 4
| let ary = ["first", "second", "third", "forth", "fifth"]; console.log(ary.slice(1,2)); console.log(ary.slice(1,3)); console.log(ary));
|
demo3:1 2 3 4 5 6 7
| let ary = ["first", "second", "third", "forth", "fifth"]; let ary1 = ary.slice(); let ary2 = ary.slice(0);
ary === ary1; ary === ary2; ary1 === ary2;
|
看出来啥猫腻没,这不是深复制一个数据吗!
最后更新时间: