array数据处理

之前写过map,forEach,现在用到every和some,记录一下当作学习笔记,方便以后翻阅。

forEach是没有返回值的,对原数组进行修改;

// forEach没有返回值,只针对每个元素调用func。
        const array = [1, 2, 3, 4];
        array.forEach((value, index) => {
            if(value > 3){
                console.log(value)/*4*/
                return false;//没有返回值,ruturn false 仍向下执行

            }else{
                console.log(value)/*1 2 3*/
                return true;
            }
        });

 map是有返回值的,可以return出来。

const array = [1, 2, 3, 4];
        let arr = [];
        arr = array.map((value, index) => {
            return value * 2;
        });
        console.log(arr);/*[2, 4, 6, 8]*/

  every:

 //every()当内部return false时跳出整个循环

const list = [10, 12, 13];
        list.every((value, index) => {
            if(value > 12){
                console.log(value) /*13*/
                return false;

            }else{
                console.log(value)/*10,12*/
                return true;/*这个return true也要写,没有的话会直接跳出循环*/
            }
        });

some:

some 当内部return true时跳出整个循环  

const array = [11, 12, 13, 14];
        array.some((value, index) => {
            if(value === 13){
                return true;//当内部return true时跳出整个循环
            }
            console.log(value)/*11 12*/
        });

  

转载于:https://www.cnblogs.com/qing619/p/11453764.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值