js处理多个if else 简化代码

优化前

                if (that.idData) {
				    uni.showToast({
					    title: '功能完善中',
					    icon: 'error',
					    duration: 1000
				    });
				    return
			    } 
				if (that.addressMap=='') {
					uni.showToast({
					    title: '请选择地址',
					    icon: 'none',
					    duration: 2000
					})
				} else if(that.detailsAddress==''){
					uni.showToast({
					    title: '请输入门牌号',
					    icon: 'none',
					    duration: 2000
					})
				}else if(that.consignee==""){
					uni.showToast({
					    title: '请输入联系人',
					    icon: 'none',
					    duration: 2000
					})
				}else if(that.consigneeTel==''){
					uni.showToast({
					    title: '请输入手机号',
					    icon: 'none',
					    duration: 2000
					})
				}else{
					console.log("判断后的数据");
				}

优化后

然后 我们通过find方法 去找到数组的每一个元素的第一个方法调用,可以看到我这里写了五个不同的条件,if也只需要写一次

            showToast(title, icon) {
			    uni.showToast({
				    title,
				    icon,
				    duration: 1000
			    });
		    },



            const rule=[
				[()=>that.idData,()=>that.showToast("功能完善中",'error')],
				[()=>that.addressMap=='',()=>that.showToast("请选择地址",'error')],
				[()=>that.detailsAddress=='',()=>that.showToast("请输入门牌号",'error')],
				[()=>that.consignee=="",()=>that.showToast("请输入联系人",'error')],
				[()=>that.consigneeTel=="",()=>that.showToast("请输入手机号",'error')],
			]
			const target = rule.find((m) => m[0]());
			if (target) {
				return target[1]();
			}
            console.log("判断后的数据");

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值