es6,find方法的使用

find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 undefined;找到满足条件,后续代码不执行。

用法

  • find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

  • 如果没有符合条件的元素返回 undefined

  • find() 对于空数组,函数是不会执行的。

  • find() 并没有改变数组的原始值。

  • array.find(function(currentValue, index, arr),thisValue),其中currentValue为当前项,index为当前索引,arr为当前数组

例子:

        let test = [1, 2, 3, 4, 5];
        let a = test.find(item => item > 3);
        console.log(a); //4

        let b = test.find(item => item == 0);
        console.log(b); //undefined

const inventory = [
    {name: 'apples', quantity: 2},
    {name: 'bananas', quantity: 0},
    {name: 'cherries', quantity: 5}
];

function findCherries(item) { 
    return item.name === 'bananas';
}

console.log(inventory.find(findCherries)); // { name: 'bananas', quantity: 0 }

判断对象里的值是否为空,以此来满足一些要求

    // 必填参数的判断
    const checkGoOrderInfoForm: keysType = {
      goDate: '去程日期',
      goDepartureTime: '去程发车时间段',
      goBeginStopId: '去程上车站点',
      goEndStopId: '去程下车站点',
      goRunId: '去程班次'
    };
    const errorKey1 = Object.keys(checkGoOrderInfoForm).find((key) => !param[key]);

    if (errorKey1) {
      Taro.showToast({
        title: checkGoOrderInfoForm[errorKey1],
        icon: 'none',
        duration: 3000
      });
      return;
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值