1. 增加

1.1 push( )

从尾部添加,可以添加多个

  1. var arr = [1,2,3,4]
  2. var b = [5,6,7,8]
  3. arr.push(5,6,7,8)
  4. console.log(arr); // [1,2,3,4,5,6,7,8]
  5. arr.push(b) // 不能实现
  6. console.log(arr);

1.2 unshift( )

从头部添加

  1. var arr=[1,2,3,4]
  2. arr.unshift(0)
  3. console.log(arr);
  4. var b=[1,2,3,4]
  5. var a = [5,6,7,8,9]
  6. a.unshift(...b)
  7. console.log(a);

1.3 concat( )

连接两个数组 (不改变原数组的内容)

  1. var arr = [1,2,3];
  2. var c = [4,5,6];
  3. var b = arr.concat(c);
  4. console.log(arr); // [1,2,3]
  5. console.log(b); // [1,2,3,4,5,6]

1.4 splice()

有增加的用法:(startIndex,0,x1,x2…)

  1. var a =[1,2,3,4]
  2. a.splice(1,0,8,9) // [1,8,9,2,3,4]
  3. console.log(a);

1.5 …arr

展开数组

  1. var arr = [1,2,3]
  2. console.log(arr);
  3. console.log(...arr); // 1 2 3
  4. var a=[4,5,6]
  5. var b = [7,8,9]
  6. a.push(...b) // 拼接数组
  7. console.log(a);

2. 删除

2.1 pop( )

从尾部删除

  1. var arr = [1,2,3,4]
  2. arr.pop()
  3. console.log(arr) // [1,2,3]

2.2 shift( )

从头部删除

  1. var arr = [1,2,3,4]
  2. arr.shift()
  3. console.log(arr); // [2,3,4]

2.3 splice()

(startIndex,长度) 定点删除

  1. var arr = [1,2,3,4,5]
  2. arr.splice(3,1)
  3. console.log(arr); // [1,2,3,5]

3. 修改

3.1 splice()

(startIndex,长度,元素…) 修改 先删除后插入

  1. var a =[1,2,3,4,5]
  2. a.splice(2,1,9)
  3. console.log(a); // [1,2,9,4,5]

4. 查询

4.1 arr[index]

查询对应下标的值

  1. //arr[index] 查询对应下标的值
  2. var arr=[1,2,3];
  3. console.log(arr[0]); //1;

4.2 indexOf(value)

查询对应值的下标

  1. var arr=[1,2,3,4];
  2. console.log(arr.indexOf(1)); // 0
  3. //如果没有对应的值,则返回-1

4.3 includes(value)

判断数组是否包含某个值

  1. // 返回boolean
  2. var arr = [1,2,3,4]
  3. console.log(arr.includes(5)); // false

4.4 slice(start,end)

选取 (包含开始,不含结束)

  1. // slice(startIndex,endIndex) 截取 包含开始不含结束
  2. // slice(startIndex) 截取从startIndex开始到后面的所有值
  3. var arr = [1,2,3,4]
  4. console.log(arr.slice(1,3)); // [2,3]
  5. console.log(arr.slice(1)); // [2,3,4]

4.5 join()

将数组拼接为字符串

  1. var arr = ["html","css","javascript"]
  2. console.log(arr.join()); // html,css,javascript
  3. console.log(arr.join("|")); // html|css|javascript
  4. console.log(arr.join("")); // htmlcssjavascript
  1. <p id="p"></p>
  2. var p = document.getElementById("p")
  3. var arr = ["1994","犯罪","剧情"]
  4. var str = arr.join("/")
  5. p.innerHTML = str
  6. console.log(p);