迭代器iterator

作者: 小雪洁 | 来源:发表于2020-03-16 14:08 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>迭代器iterator</title>
        </head>
        <body>
        </body>
        <script>
            let names=["hxj","ydc","hwx"];
            let keys =names.keys();
            console.log(keys);//返回一个迭代对象
            console.log(keys.next());//{value: 0, done: false} 
            //value值指的是索引,done值表示是否迭代完成,false表示还没迭代完
            //console.log(keys.next());//{value: 1, done: false}
            //console.log(keys.next());//{value: 2, done: false}
            //console.log(keys.next());//{value: undefined, done: true}迭代完成
            let arr=["hxj","ydc","hwx"];
            //let values = arr.values();//报错?
            let entries = arr.entries();
            console.log(entries);//返回一个迭代对象
            console.log(entries.next());//{value: Array(2), done: false}
            //其中Array(2):[0,"hxj"],一个是索引,一个是值
            //使用展开语法读取entries.next()里的值
            //let {done,value}=entries.next();
            //console.log(done,value);// [1, "ydc"]
            //使用解构方法读取数组变量
            let {done,value:[a,b]}=entries.next();
            console.log(a,b);
        </script>
    </html>
    
    

    相关文章

      网友评论

        本文标题:迭代器iterator

        本文链接:https://www.haomeiwen.com/subject/rlegehtx.html