1、扩展运算符

  1. <script>
  2. let arrays = [1, 2, "abc"];
  3. console.log(...arrays);
  4. </script>

2、替代apply方法

  1. <script>
  2. //从一个数组中找出最大的数
  3. let arrays = [1, 2, 3, 4, 5];
  4. //es5的写法
  5. console.log(Math.max.apply(null, arrays));
  6. //es6的写法
  7. console.log(Math.max(...arrays));
  8. </script>

3、数组合并

  1. <script>
  2. //合并数组
  3. let arrays1 = [1, 2, 3, 4, 5];
  4. let arrays2 = [6, 7, 8, 9, 10];
  5. //es5的写法
  6. console.log(arrays1.concat(arrays2));
  7. //es6的写法
  8. console.log([...arrays1, ...arrays2]);
  9. </script>

4、Array.from方法

作用:将伪数组转为真正的数组
伪数组有:1、arguments 2、元素集合 3、类似数组的对象

  1. <script>
  2. // 1、arguments
  3. function add(){
  4. console.log(arguments);
  5. console.log(arguments[0]);
  6. console.log(arguments[1]);
  7. console.log(arguments[2]);
  8. //arguments.push(40); //error arguments.push is not a function
  9. //把arguments转化为真正可操作的数组
  10. let arrays = Array.from(arguments);
  11. arrays.push(40);
  12. arrays.push(50);
  13. console.log(arrays);
  14. }
  15. add(10,20,30);
  16. </script>
  1. <body>
  2. <div></div>
  3. <div></div>
  4. <div></div>
  5. <script>
  6. // 2、元素集合
  7. let divs = document.getElementsByTagName("div");
  8. console.log(divs);
  9. //divs.push(10); //error divs.push is not a function
  10. // 把元素集合转化为可操作的数组
  11. let arrays = Array.from(divs);
  12. arrays.push(10);
  13. arrays.push(20);
  14. console.log(arrays);
  15. </script>
  16. </body>
  1. <script>
  2. // 3、类似数组的对象
  3. let user = {
  4. "0":"azhi",
  5. "1":18,
  6. "2":"男",
  7. length:3
  8. }
  9. let arrays = Array.from(user);
  10. arrays.push(80);
  11. console.log(arrays);
  12. </script>

5、Array.of方法

将一组值转化为数组

  1. <script>
  2. let arrays = Array.of(1,2,3,4,5,6,7,8);
  3. console.log(arrays);
  4. </script>