Use a for loop to check whether a required field is empty

When submitting information, it is often necessary to judge whether the required items of user information are empty at the front end. At this time, there are often multiple if judgments, which is too cumbersome to write. In order to simplify the process, a for loop is used to judge whether it is free. Item is a better way.

let mustData = ['name','address','trademarkImg','topImg','middleImg','phone','contacts','contactsPhone'];
let mustDataTip = ['店铺名称不能为空','详细地址不能为空','请上传门脸照','请上传店内照','请上传门店LOGO','外卖电话不能为空','联系人不能为空','联系电话不能为空'];
let canSet = true;
for(let i=0;i<mustData.length;i++) {
	if(this.form[mustData[i]] == '') {
			uni.showToast({
				title: mustDataTip[i],
					icon: 'none',
					duration: 2000
				});
				canSet = false;
				break;
			}
	};
return canSet;

This is just a better solution than the if judgment, if you have a better solution, please leave a message below

Guess you like

Origin blog.csdn.net/weixin_42252416/article/details/106968387