How do I check if all elements of an array are null?

前端 未结 3 482
生来不讨喜
生来不讨喜 2020-12-18 09:46

What\'s the best way to check for an array with all null values besides using Lodash, possibly with ES6?

var emp = [null, null, null];
if (_.compact(emp).len         


        
3条回答
  •  被撕碎了的回忆
    2020-12-18 09:48

    You can use Array#every or lodash's _.every() with _.isNull():

    var emp = [null, null, null];
    
    var result = emp.every(_.isNull);
    
    console.log(result);

提交回复
热议问题