1 数组的长度
数组的长度有属性length,值是这个数组的最大整数属性名+1
const arr = []
console.log(arr.length); // 0
arr[1000] = true
console.log(arr.length); // 1001
2 数组的删除
(1)使用delete
缺点:删除的位置会有空缺
const arr2 = [1, 2, 3, 4]
delete arr2[2]
console.log(arr2); // [ 1, 2, <1 empty item>, 4 ]
(2) 使用splice
删除的位置没有空缺,后面的元素往前推
const arr3 = [1, 2, 3, 4]
arr3.splice(2, 1)
console.log(arr3); // [1, 2, 4]
数组的常用函数有push forEash, join, concat, filter, map等等,这里不多赘述
3 数组的枚举
JavaScript的枚举其实是Array-like(类数组),使用for in 遍历无法保证是按照阿拉伯数字遍历,所以避免使用for in 语法,而使用for循环遍历
const arr4 = [1, 2, 3, 4]
arr4['a'] = true
console.log(typeof arr4); // Object 数组的类型是对象
console.log(arr4.constructor === Array); // true 而构造函数却是数组
for(item in arr4) {
console.log(item);
} // 0 1 2 3 a
for (let i = 0; i < arr4.length; i++) {
const a = arr4[i];
console.log(a);
} // 1 2 3 4