03JavaScript程序设计修炼之道 2019-06-02_15-20-09 for each 数组es6的方法

25array4.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // forEach()
        var arr = [11,3,56,56,34,100,9,100];
        /*
        arr.forEach(function(item,index) {
            console.log(index+"-"+item);
        });
        */
        arr.forEach((item,index)=>{
            //console.log(index+"-"+item);
            if(item%2===0) {
                console.log(item);
            }
        });
        // 求上述数组最大值
        var max = arr[0];
        arr.forEach((item)=>{
            if(item>max) {
                max = item;
            }
        });
        console.log(max);

        // indexOf() 查找元素在数组中第一次出现的索引 假如不存在返回值-1
        var index = arr.indexOf(200);
        var index2 = arr.lastIndexOf(100); //从右往左找
        console.log(index);//4
        console.log(index2); // 6
        // 对数组进行去重
        function noRepeat(arr) {
            var newArr = [];
            // 对arr循环 每一次拿到元素 判断是否在newArr中 没有就加到newArr
            arr.forEach((item)=>{
                if(newArr.indexOf(item) === -1) {
                    newArr.push(item);
                }
            });
            return newArr;
        }
        console.log(noRepeat(arr));

        // map filter reduce some every 不会改变原数组
        var array = [12,14,5,6,32,10];
        /*var newArr = array.map((item)=>{
            return item*1.2;
        });
        // filter过滤 由函数返回值为true的元素所组成
        var newArr = array.filter((item)=>{
            return item>12;
        });
        */
        // reduce 收敛
        /*
            第一次 prev 12  next 14
            第二次 prev undefined next 5
            第三次 prev undefined next 6
            ...   

            从第二次开始的prev 是上一次的函数返回值
        */
        var sum = array.reduce((prev,next)=>{
            console.log(prev+"---"+next);
            return prev+next;
        });
        // 当reduce传入第二个参数 第一次的prev指向第二个参数
        var sum2 = array.reduce((prev,next)=>{
            console.log(prev+"---"+next);
            return prev+next;
        },6);
        console.log(sum2);
        //console.log(newArr,array);
        // some 返回true/false
        var arr2 = [10,4,5,6,11,20]; //
        var res = arr2.some((item)=>{
            console.log(item);
            return item>100;
        });
        console.log(res); // true
        console.log("--------------");
        // every 直到第一个为false停止循环 否则返回true
        var res2 = arr2.every((item)=>{
            console.log(item);
            return item>10;
        });
        console.log(res2);
    </script>
</body>
</html>

26array5.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // Math.pow()
        // es6新增的 from 把数组或类数组变成数组 会复制一份
        var arr = [1,2,3];
        var newArr = Array.from(arr);
        console.log(newArr);
        // 类数组 并不是真正的数组 而是对象 arguments
        function fn() {
            //arguments.push(10); 报错
            var arguments = Array.from(arguments);
            arguments.push(12);
            console.log(arguments instanceof Array);
        }
        fn(1,2,3);
        //console.log(arr instanceof Array);
        var obj = {
            "0": 10,
            "1":20,
            length: 2
        }
        var arr2 = Array.from(obj);
        console.log(arr2);
        // of fill copyWithin
        var array = new Array(3); //  var array = [3];
        var array1 = new Array(3,4);
        var array2 = Array.of(3,4);// 3是元素
        console.log(array,array.length);
        console.log(array1,array1.length);
        console.log(array2,array2.length);
        // fill copyWithin
        var res = [1,2,3,4].fill(5,1,3); // [1,5,5,4]
        var res2 = [1,2,3,4].copyWithin(1,0,2); // 用序号为0的元素一直到序号为2但不包含2的元素 1 2
        console.log(res);
        console.log(res2);// [1,1,2,4]
        // es7 includes
        console.log([10,12,23,43].includes(100));// false 不存在 true存在
        // find findIndex (keys values)
        var find = [1,2,3,4,5,3].find((item,index,arr)=>{
            console.log("fn");
            return item==13;
        });
        var findIndex = [1,2,3,4,5,3].findIndex((item,index,arr)=>{
            return item==3;
        });
        console.log(findIndex);
    </script>
</body>
</html>

27array6.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <script>
        // 解构赋值 解析数组或对象的结构为不同的变量提取值
        //var name = "along";
        //var age = 32;
        var [name,age]  = ["along",32];
        console.log(name);
        console.log(age);
        //var [x,y,z]   = [1,[2.2,2.3]];
        //var [x,[y,z]]   = [1,[2.2,2.3]];
        //console.log(x,y,z); // 1 [2.2,2.3] undefined
        var [json,arr,num] = [{a:10,b:20},[1,2,3],10];

        var [,,x=4]  = [1,2];
        console.log(x);
        var [x,...y] = [10,20,30,40];// 剩余项
        console.log(x,y);// 10 [20,30,40]

        var arr = [1,2,3];
        var [...arr1] = arr; // 克隆数组
        console.log(arr1);

        var x  = 10, y = 20;
        /*
        var temp = x;
        x = y;
        y = temp;
        */
        [x,y] = [y,x];
        console.log(x,y);
     </script>
</body>
</html>

原文地址:https://www.cnblogs.com/HiJackykun/p/11135520.html

时间: 2024-09-29 21:36:26

03JavaScript程序设计修炼之道 2019-06-02_15-20-09 for each 数组es6的方法的相关文章

03JavaScript程序设计修炼之道 2019-05-19_14-33-19--05-19_15-09-07 js简介与基本使用、变量与常量

es.md JavaScript程序设计修炼之道 -前置知识 html5+css3 -课程大纲 1.客户端原生js(es6/es7/..) 2.js常用库 jq等 3.服务端js(node/express/koa) 4.前端js框架(vue/react/..) ... -课程地位 学好js,走遍天下都不怕,是web前端或web全栈开发最重要最核心的技术,直接决定职业收入和发展 客户端原生javascript(es5/es6/es7/...) js简介与基本使用 html 标记语言 css jav

03JavaScript程序设计修炼之道 2019-06-09_fe-js-053Math对象_ Date对象、es6对象新变化

36Date.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatib

03JavaScript程序设计修炼之道-2019-06-18_20-07-52-Dom 文档对象模型

原文地址:https://www.cnblogs.com/HiJackykun/p/11053883.html

03JavaScript程序设计修炼之道-2019-06-20_20-07-53

06changeColor.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-C

03JavaScript程序设计修炼之道 2019-06-23_15-50-11 验证码

1.png tool.js function rand(min,max) { return Math.round(Math.random()*(max-min)+min); } function $(id){ return document.getElementById(id); } 验证码.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/

03JavaScript程序设计修炼之道 2019-06-27_20-04-17 节点元素操作:复制

15clone.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compati

03JavaScript程序设计修炼之道 2019-06-25_20-34-21-2019-06-25_20-44-06 节点元素的操作:增删改

14nodeOper.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Comp

03JavaScript程序设计修炼之道 2019-05-23_20-28-51_2019-05-26_21-12-07 for、练习、hw

16for.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatibl

03JavaScript程序设计修炼之道 2019-06-04_fe-js-044es6解构赋值之数组解构_2019-06-06_20-10-17 解构赋值、数组排序法

27array6.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compat