< meta charset = " UTF-8 " > < meta name = " viewport " content = " width=device-width, initial-scale=1.0 " > < title > 用foreach()方法把类数组对象转换为数组 </ title > < script > window . onload = function ( ) { var odivs = document . getElementsByTagName ( "div" ) ; console . log ( odivs ) var odivs2 = Array . prototype . forEach . call ( odivs , function ( item , index ) { console . log ( item , index ) ; </ script > </ head > < div > div1 </ div > < div > div2 </ div > < div > div3 </ div > < div > div4 </ div > </ body > </ html >

2.slice()方法

js代码:
  window.οnlοad=function(){
            var odivs=document.getElementsByTagName("div");
            console.log(odivs)
            odivs=Array.prototype.slice.call(odivs);
            odivs1=[].slice.call(odivs);
            console.log(odivs)
            console.log(odivs1)
html代码:
  <div id="one">div1</div>
    <div id="two">div2</div>
    <div id="three">div3</div>

3.map()方法

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>map转换为数组</title>
    <script>
        window.onload=function(){
            var odivs=document.getElementsByTagName("div")
            console.log(odivs)
            //第一种:
            var odivs1=Array.prototype.map.call(odivs,function(item,index){
                return item;
            //第二种:
            // var odivs1=[].map.call(odivs,function(item,index){
            //     return item;
            // })
            console.log(odivs1)
    </script>
</head>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</body>
</html>