简体   繁体   English

js检查许多不等于空的变量

[英]js check many variables not equal to empty

Maybe this is a duplicate question. 也许这是一个重复的问题。 But I didn't find any good answer at all. 但是我根本找不到任何好的答案。 I want to check multiple variables if not equal to empty('') as below; 我想检查多个变量,如果不等于empty(''),如下所示;

var list1 = '';
var list2 = 'something';
var list3 = '';
var list4 = 'hello world';

Now I want to check only one variable not equal empty returns true. 现在,我只想检查一个不等于空的变量,返回true。 If more than one variables are not empty returns false 如果多个变量不为空,则返回false

As I tried 当我尝试

if(list1 != '' && list2 != '' && list3 != '' && list4 != ''){
   alert("ALL are filled");
   return false;
}

I have no idea how to do it with javascript logical operations 我不知道如何使用javascript逻辑操作

You can create an array and use filter + length to get the number of empty items: 您可以创建一个数组,并使用filter + length来获取空项目的数量:

 var list1 = ''; var list2 = 'something'; var list3 = ''; var list4 = 'hello world'; var list = [list1, list2, list3, list4]; var numberEmptyItens = list.filter((item) => item === '').length; console.log(numberEmptyItens); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM