JS学习笔记 - - day07

一、数组迭代器方法 – 产生新的数组

1.map()方法,返回值是新数组,会将return返回值添加到新数组中
map(function(ele,index,arr){

})

map方法和forEach方法区别:
map方法会有返回值,返回值是新数组,新数组中的元素是将return后面的返回值, forEach方法没有返回值

2.filter(),返回值是新数组,会将return之后满足条件的元素添加到新数组中
filter(function(ele,index,arr){

})

二、reduce()方法的使用

1.reduce(function(prev,next,index,arr){},defalutvalue)
prev表示上一次的返回值
next表示下一个元素
index表示下标(next的下标)
arr表示原数组
defalutvalue:设置prev默认值,不设置prev表示数组中第一个元素

1.元素累加

<script>
 var arr = [1, 2, 10, 15, 7, 9];
 // 数组元素累加
        var res = arr.reduce(function (prev, next, index, arr) {
            // console.log(prev, next, index, arr);
            console.log(prev,next);//1 2 / 3 10 /  13 15
            return prev + next
        })
</script>

2.元素累乘

<script>
 // reduce练习1:数组元素累乘
        var res = arr.reduce(function (prev, next, index, arr) {
            console.log(prev,next);
            return prev * next
        },1)

</script>

3.求最大值

<script>
var arr = [1, 2, 10, 15, 7, 9];
        // reduce练习2:求数组中的最大值
        var res = arr.reduce(function (prev, next) {
            return prev >next ? prev : next
        })
        console.log(res);
</script>

4.购物车计算总价

<script>
 var foodlist = [
            {
                title:"鱼香肉丝",
                price:18,
                count:2
            },{
                title:"麻婆豆腐",
                price:10,
                count:1
            },{
                title:"干锅排骨",
                price:88,
                count:1
            },{
                title:"米饭",
                price:2,
                count:5
            }
        ]
        var res = foodlist.reduce(function(prev,next){
            console.log(prev,next);// 0 {} // 36  {}
            return prev + next.price*next.count
        },0)

        console.log(res);
</script>
三、数组其他方法

1.判断数据是否为数组
Array.isArray()方法

2.将类数组转换为真的的数组
类数组: arguments/获取页面中所有的节点
Array.from()

3.reverse()方法:反转数组中的元素,会影响原数组

  1. slice(startindex,endindex) : 截取数组
    支持负数: -1表示最后一个元素,-2表示倒数第二个元素
    startindex:开始截取的位置
    endindex(可选):结束的位置
    slice(startinex) [startinex,length)
    slice(startinex,endindex) [startinex,endindex)
四、Math对象方法

1.Math.PI(Π常数)
2.向上取整:Math.ceil()
3.向下取整:Math.floor()
4.四舍五入:Math.round()
5.随机数:Math.random() [0,1)
6.最大值:Math.max()
7.最小值:Math.min()
8. x的y次幂:Math.pow(x,y)

随机数练习

<script>
 // 随机数练习1: [0,10] 之间的随机整数
        console.log(Math.floor(Math.random() * 11)); //[0,11)   10.9999

        var arr = ["a", "b", "c", "d", "e", "f"];

        // 随机数练习2: 随机点名
        var index = Math.floor(Math.random() * 6);
        document.write(arr[index])

        // 随机数练习3: 给body标签设置随机色  rgb [0,255]
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);

        document.querySelector("body").style.backgroundColor = `rgb(${r},${g},${b})`;

        // 随机数练习4: 随机生成4位数的验证码
        var str = "123456789QWERTYUIOPASDFGHJKLZMNXBCVqwertyuioplaksjdhfgzmxncbv";

        var arr = [];
        for (var i = 0; i < 4; i++) {
            var index = Math.floor(Math.random() * 61); //[0,60]
            arr.push(str[index])
        };

        console.log(arr.join(""));
</script>
时间对象方法
 <script src="">
        // 1.年份
        console.log(time.getFullYear()); //2022
        // 2.月份 [0,11]
        console.log(time.getMonth() + 1); //12
        // 3.日期
        console.log(time.getDate()); //29
        // 4.星期几 [0,6] 0表示星期天
        console.log(time.getDay()); //4
        // 4.小时
        console.log(time.getHours()); //16
        // 5.分钟
        console.log(time.getMinutes()); //6
        // 6.秒
        console.log(time.getSeconds());
        // 7.时间戳(1970/1/1 00:00:00 到 现在 的总毫秒数)
        console.log(time.getTime());
        console.log(time.valueOf()); //时间对象的原始值是时间戳
        // 8.指定时间
        console.log(new Date("2023/1/1")); //Sun Jan 01 2023 00:00:00 GMT+0800 (中国标准时间)
        console.log((new Date("2023/1/2 14:12:00")).getDay());
    </script>
<script>
        setInterval(function () {
            // 计算时间差
            var nowtime = new Date();
            var eattime = new Date("2022/12/29 17:30:00");
            // 引用数据类型,先调用valueOf()方法获取原始值,原始值不是基本数据类型,在调用toString()
            console.log(eattime - nowtime);
            var time = eattime - nowtime; //时间差
            
            var hours = Math.floor(time / 1000 / 60 / 60);
            hours = hours < 10 ? "0" + hours : hours

            var minitus = Math.floor(time / 1000 / 60 % 60);
            minitus = minitus < 10 ? "0" + minitus : minitus

            var seconds = Math.floor(time / 1000 % 60);
            seconds = seconds < 10 ? "0" + seconds : seconds

            var mybox = document.querySelector("div");
            mybox.innerHTML = `计算目标时间还有${hours}小时${minitus}分钟${seconds}`
            // console.log(seconds);
        }, 1000)
    </script>
定时器

setTimeout(function(){},毫秒数) 隔多少毫秒只执行一次
setInterval(function(){},毫秒数) 每隔多少毫秒数都会执行一次function中的代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[removed] $(function() { function setCurrentSlide(ele, index) { $(".swiper1 .swiper-slide").removeClass("selected"); ele.addClass("selected"); //swiper1.initialSlide=index; } var swiper1 = new Swiper('.swiper1', { // 设置slider容器能够同时显示的slides数量(carousel模式)。 // 可以设置为number或者 'auto'则自动根据slides的宽度来设定数量。 // loop模式下如果设置为'auto'还需要设置另外一个参数loopedSlides。 slidesPerView: 5.5, paginationClickable: true,//此参数设置为true时,点击分页器的指示点分页器会控制Swiper切换。 spaceBetween: 10,//slide之间的距离(单位px)。 freeMode: true,//默认为false,普通模式:slide滑动时只滑动一格,并自动贴合wrapper,设置为true则变为free模式,slide会根据惯性滑动且不会贴合。 loop: false,//是否可循环 onTab: function(swiper) { var n = swiper1.clickedIndex; } }); swiper1.slides.each(function(index, val) { var ele = $(this); ele.on("click", function() { setCurrentSlide(ele, index); swiper2.slideTo(index, 500, false); //mySwiper.initialSlide=index; }); }); var swiper2 = new Swiper('.swiper2', { //freeModeSticky 设置为true 滑动会自动贴合 direction: 'horizontal',//Slides的滑动方向,可设置水平(horizontal)或垂直(vertical)。 loop: false, // effect : 'fade',//淡入 //effect : 'cube',//方块 //effect : 'coverflow',//3D流 // effect : 'flip',//3D翻转 autoHeight: true,//自动高度。设置为true时,wrapper和container会随着当前slide的高度而发生变化。 onSlideChangeEnd: function(swiper) { //回调函数,swiper从一个slide过渡到另一个slide结束时执行。 var n = swiper.activeIndex; setCurrentSlide($(".swiper1 .swiper-slide").eq(n), n); swiper1.slideTo(n, 500, false); } }); }); [removed]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值