前端基础学习

迭代器iterator

2020-03-16  本文已影响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>

上一篇下一篇

猜你喜欢

热点阅读