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

前端 未结 3 470
生来不讨喜
生来不讨喜 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:58

    Another way to solve this. Is join the array elements and replace the left out ',' with empty space.

    var emp = [null, null, null];
    
    if(emp.join(',').replace(/,/g, '').length === 0) {
     console.log('null');
    }

提交回复
热议问题