ES6 数组API的实现(练习笔记)

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!--
         ES数组API

         forEach
         map
         find 与 findIndex
         filter
         some every
         reduce
  
     -->
    <script>
        // reduce : 对数组的元素值进行累计(元素值进行加减乘除)
        // acc: 累计数 上一个元素值与初始的数据进行累计的结果
        // cur: 当前循环遍历的元素
        // idx: 当前循环遍历的数组下标
        // target:循环遍历的数组对象
        // initvalue: 累计的初始值,如果该参数未提供,则累计的初始值以数组第一个元素值为累计的初始值
        // 注:initvalue在正式循环遍历过程中自遍历的次数 数组长度-1
        // reduce函数的返回值 数组所有元素累计的结果值
        // Array.reduce((acc,cur,idx,target) =>{},initvalue);
        const arr1 = [1, 2, 3];
        const init = 10;
        let totals = arr1.reduce((acc, cur, idx, target) => {
            // console.log('acc', acc, ' cur:', cur, ' idx:', idx, ' target', target);
            return acc + cur;
        }, init);
        // console.log(totals);

        let shoppingcart = [{
            sn: '001',
            sname: 'Java入门',
            price: 85,
            count: 10
        }, {
            sn: '002',
            sname: 'JS入门',
            price: 35,
            count: 5
        }, {
            sn: '003',
            sname: 'HTML入门',
            price: 55,
            count: 6
        }, ];
        let totalCount = shoppingcart.reduce((acc, cur) => {
            // console.log('acc', acc, ' cur:', cur);
            return acc + cur.price * cur.count
        }, 0);

        // console.log('当前订单总金额:', totalCount);

        //函数式编程: 提供给使用一个函数对象, 使用者可以将业务封装在该函数对象中,内部执行该函数对象完成使用者的业务设计
        //prototype : js对象中内置的属性 给所有的对象实例添加共享的属性值
        Array.prototype.custForEach = function(callback) {
            //this 获取当前循环遍历的数组对象
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                callback(this[index], index, this);
            }
        }

        // 模拟map 函数
        Array.prototype.custMap = function(callback) {
            // 定义一个新数组对象 用map 函数返回一个新的数组
            let mapArray = [];
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                let mapValue = callback(this[index], index, this);
                //将 返回值 mapValue 添加至新数组对象中mapAray
                mapArray.push(mapValue);
            }
            return mapArray;
        }

        //模拟 find 函数
        Array.prototype.cutFind = function(callback) {
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                let findFlag = callback(this[index], index, this);
                if (findFlag) return this[index];


            }
            return undefined;

        }

        //模拟 find 函数
        Array.prototype.cutFindIndex = function(callback) {
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                let findFlag = callback(this[index], index, this);
                if (findFlag) return index;


            }
            return -1;

        }

        //模拟 filter
        Array.prototype.custFilter = function(calback) {
            let tempArray = [];
            for (let index = 0; index < this.length; index++) {

                //每次循环调用函数模块
                let findFlag = callback(this[index], index, this);
                if (findFlag) tempArray.push(this[index]);
            }
            return tempArray;
        }

        //模拟Some
        Array.prototype.custSome = function(callback) {
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                let findFlag = callback(this[index], index, this);
                if (findFlag) return true
            }
            return false;
        }

        //模拟 every
        Array.prototype.custEvery = function(callback) {
            for (let index = 0; index < this.length; index++) {
                //每次循环调用函数模块
                let findFlag = callback(this[index], index, this);
                if (!findFlag) return false
            }
            return true;
        }


        let arr0 = [1, 2, 3];
        // //定义函数对象
        // arr0.custForEach(((item, idx, target) => {
        //     // console.log('item: ', item, ' index:', idx, ' target', target);
        // }))

        // let tempMapArray = arr0.custMap((item, idx, target) => {
        //     return item * 2
        // })
        // console.log(tempMapArray);

        // let findObj = arr.custFind((item, index, target) => {
        //     return 4 == item
        // });
        // console.log(findObj);

        // let tarry = arr0.custFilter((item, idx, target) => {
        //     return item < 3
        // })
        // console.log('tarry:', tarry);

        // let flag = arr0.custSome((item, idx, target) => {
        //     return item == 0;
        // })
        // console.log(flag);

        let flag = arr0.custEvery((item, idx, target) => {
            return item < 4;
        })
        console.log(flag);
    </script>

</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值