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).length == 0) { ... }
A side note: Your solution doesn't actually check for an all-null array. It just checks for an all-falsey array. [0, false, ''] would still pass the check.
[0, false, '']
You could use the Array#every method to check that every element of an array meets a certain condition:
Array#every
const arr = [null, null, null]; console.log(arr.every(element => element === null));
2.1m questions
2.1m answers
60 comments
57.0k users