伪数组转换为真数组的方法

伪数组转换为真数组的方法
1、let newArr = Array.protype.slice.call(伪数组)
2、let newArr = Array.from(伪数组),ES6的新语法
3、let newArr = [...伪数组],使用扩展运算符,也是ES6的语法

 

<body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
    </ul>
    <script>
       那些是伪数组
      //  arguments
      // BOM的对象列表  document.querySelectorAll() document.getElementById() childNodes

      //  Array.from(lis) 把伪数组转换为真数组
      const lis = document.querySelector("li");

      const trueLi = Array.from(lis); //转换成真数组
      console.log(trueLi);
      trueLi.push("1"); //可以使用数组的方法
    </script>
  </body>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
posted @ 2022-11-24 23:54  噢噢噢J  阅读(35)  评论(0编辑  收藏  举报