展开语法

作者: 小雪洁 | 来源:发表于2020-03-17 17:36 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>展开语法</title>
        </head>
        <body>
            <script>
                //合并数组
                let hxj =["hao","xue","jie"];
                let ydc=["yang","ding","chuan"];
                console.log(hxj);// ["hao", "xue", "jie"]
                console.log(...hxj);//hao xue jie
                //使用for-in循环ydc追加到hxj
                /* for(let key in ydc){
                    hxj.push(ydc[key]);
                }
                 console.log(hxj);//["hao", "xue", "jie", "yang", "ding", "chuan"]
                */
                //注意数组循环for-of 里的循环变量直接就是值
                /* for(const value of ydc){
                    hxj.push(value);
                } 
                console.log(hxj);//["hao", "xue", "jie", "yang", "ding", "chuan"]
                */
                //使用展开语法合并数组
                let arr=[...hxj,...ydc];
                console.log(arr);
                //使用展开语法传递多个参数
                function sum(...args){
                    return args.reduce((s,i)=>{
                        return (s += i);
                    },0)
                }
                console.log(sum(1,2,3));//6
                
            </script>
        </body>
    </html>
    
    

    相关文章

      网友评论

        本文标题:展开语法

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