ES6

ES6 - for of循环

2019-01-07  本文已影响0人  饮杯梦回酒

导读 :

例子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // forEach()  注意点:使用箭头函数第二个参数(指向问题)将不生效 
        /*      
        let arr = ['apple','banana','orange'];
        arr.forEach(function(val, index, arr){
            console.log(this, val, index, arr);
        },document);
        */


        // map()  注意点:与forEach最大区别在于该函数可以设置返回值格式
        /*      
        let arr = [
            {title: 'aaa', read: 100, hot: true},
            {title: 'bbb', read: 567, hot: false},
            {title: 'ccc', read: 154, hot: false},
            {title: 'ddd', read: 5, hot: true}
        ];
        let res = arr.map(function(item, index, arr){
            let json = {};
            json.t = `^_^${item.title}`;
            json.r = item.read + 100;
            json.h = item.hot == true && '真棒!!!';
            return json;
        });
        console.log(res);
        */


        // filter  注意点:过滤符合要求的组
        /*
        
        let arr = [
            {title: 'aaa', read: 100, hot: true},
            {title: 'bbb', read: 567, hot: false},
            {title: 'ccc', read: 154, hot: false},
            {title: 'ddd', read: 5, hot: true}
        ];
        let newArray = arr.filter(function(val, index, arr){
            return val.hot == true;
        });
        console.log(newArray);
        */


        // some   注意点:判断有没有包含指定的值
        /*  
        let arr = ['apple','banana','orange'];
        let temp = arr.some((val, index, arr)=>{
            return val == 'banana';
        });
        console.log(temp); // true*/


        // every   注意点:和some相似,只不过是满足所有条件为true
        // reduce  求和,阶乘等(Math.pow())
        // ES6新增: for..of       遍历索引和值:arr.entries(),索引:arr.keys()
        let arr = ['apple','banana','orange'];
        for(let val of arr) {
            console.log(val);
        }

    </script>
</body>
</html>

总结 :

上一篇 下一篇

猜你喜欢

热点阅读