I have an array with a length of 12. how can I check all the arrays of the array are empty?
let array = [[],[],[],[],[],[],[],[],[],[],[],[]]
On each element, you could do a strict check with Array.isArray (to avoid empty string "" and object {length: 0}) and then check its length
const array = [[],[],[],[],[],[],[],[],[],[],[],[]]
const falseArray1 = [[1],[],[],[],[],[],[],[],[],[],[],[]]
const falseArray2 = ["",[],[],[],[],[],[],[],[],[],[],[]]
const falseArray3 = [{length:0},[],[],[],[],[],[],[],[],[],[],[]]
const isValid = arrayOfArray => arrayOfArray.every(arr => Array.isArray(arr) && arr.length === 0)
console.log(isValid(array))
console.log(isValid(falseArray1))
console.log(isValid(falseArray2))
console.log(isValid(falseArray3))
You just need to iterate through every array and checks if all the sub-arrays are empty
array.every(subArr => subArr.length === 0)
I think the easiest approach would be to create a function, i,e:
function checkEmpty(array) {
return Array.isArray(array) && (array.length == 0 || array.every(isEmpty));
}
You can then test which arrays you'd like. For example:
console.log(checkEmpty([[], [[]]]));
Output:
true
You can then use this boolean value to your advantage and work with anything else you'd like.
array.every(arr => arr.length < 1)work?arr.flat().length == 0array.join('') === ""