首页 > 技术文章 > 利用rest 参数进行数组添加

qdlhj 2019-07-02 18:28 原文

rest参数中的变量代表一个数组,所有数组特有的方法都可以用于这个变量:

function push(array, ...items) {
    items.forEach(function(item) {
      array.push(item);
      console.log(item);
    });
  }
  let  a = [];
  push(a, 1, 2, 3)
  console.log(a)  //输出 [1, 2, 3]

 

推荐阅读