11

I have an array like:

["", "", "", "1", "", ""]

I want to alert when all the array values are blanks, i.e, when the array is like this:

["", "", "", "", "", ""]

How can I achieve this.

Shidersz
17.2k2 gold badges28 silver badges52 bronze badges
asked May 27, 2019 at 4:31
5
  • Possible duplicate of How to watch for array changes? Commented May 27, 2019 at 4:36
  • 1
    How is it a duplicate @Faisal? Commented May 27, 2019 at 4:36
  • I think the description is more suited to describe observable arrays. Commented May 27, 2019 at 4:38
  • 1
    But it is not be duplicate @Faizal. Commented May 27, 2019 at 4:39
  • Erm...the above doesn't need to observe anything @Faisal Commented May 27, 2019 at 4:51

10 Answers 10

12

Use every():

const allEmpty = arr => arr.every(e => e === "");
console.log(allEmpty(["", "", "", "1", "", ""]));
console.log(allEmpty(["", "", "", "", "", ""]));

answered May 27, 2019 at 4:34
Sign up to request clarification or add additional context in comments.

2 Comments

Just out of curiousity: is it better to use == or === for this case, or does it not really matter at all?
Whoops - I missed that @Terry. Thanks! It's better to use === because if an array element is 0, then it'll be removed (because 0 == ""). Try it - it evaluates to true! (Weird, I know - but they both evaluate to false)
11

Try this,

["", "", "", "", "", ""].join("").length==0

If you want to remove spaces,

["", "", "", "", "", ""].join("").replace(/\s/gi,'').length==0

Note :

This will not work for inputs like ["", [], "", null, undefined, ""]

answered May 27, 2019 at 4:34

8 Comments

You can also trim() if there are spaces in the array :)
array.join("").replace(/\s/gi,'').length==0 will be improved version of this answer.
I dont want to trim because that blank values i have to use
I hope he wasn't worried about spaces , updated the answer anyway. @VikashSingh
Note this will returns true for an input like: ["", [], "", null, undefined, ""]. If you are strictly looking that all items are empty strings "" then this won't be good.
|
6
TL;DR (Fastest & Simple)
[].some(Boolean) // Empty : false | Not Empty : true
Explanation

Mixture of native functions, Boolean with .some() or .filter() or using .join() can return the expected result:

var a=['','','','',''], // Empty
 b=['','','x','','']; // Not Empty
// Is Not Empty?
// #1
console.log( a.some(Boolean) ); // false
console.log( b.some(Boolean) ); // true
// #2
console.log( a.filter(Boolean).length ); // = 0
console.log( b.filter(Boolean).length ); // != 0
// #3
console.log( a.join('')!='' ); // false
console.log( b.join('')!='' ); // true

Also a user-defined function:

var a=['','','','',''], // Empty
 b=['','','x','','']; // Not Empty
 
function isArrayEmpty(a) { 
 for (var i = 0; i < a.length; i++) 
 if (a[i]) return false;
 return true;
}
console.log( isArrayEmpty(a) ); // true
console.log( isArrayEmpty(b) ); // false

But about performance:

  1. .some(Boolean) ~40M ops/s (Operation per second)
  2. isArrayEmpty() ~36M ops/s (~10% slower)
  3. .filter(Boolean).length ~9M ops/s (~75% slower)
  4. .join('')!='' ~4M ops/s (~90% slower)

Note (1): Using Arrow functions (a)=>a instead of Boolean, will make the performances even lower, in these cases about ~5% slower.

Note (2): The expected result is same just when we are sure that all of array items are String. About other falsy s (null or false ...) items, the option #3 (join) will not work.

answered Jan 17, 2022 at 0:22

1 Comment

Easily the best answer here. This should be the accepted answer.
4

You can always use a basic for loop as a solution for your problem:

function allBlanks(arr)
{
 for (var i = 0; i < arr.length; i++)
 {
 if (arr[i] !== "") return false;
 }
 return true;
}
console.log(allBlanks(["", "", "", "1", "", ""]));
console.log(allBlanks(["", "", "", "", "", ""]));
console.log(allBlanks(["", [], "", null, undefined, ""]));
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

answered May 27, 2019 at 5:26

Comments

3

Here is also a more generic approach which would compact the array removing all falsey values and then check the remaining length:

let compact = a => a.reduce((r,c) => (!!c ? r.push(c) : null, r),[])
let isEmpty = array => compact(array).length == 0
console.log(isEmpty(["", false, 0, "", null, undefined])) // true
console.log(isEmpty(["", 1])) // false
console.log(isEmpty(["", []])) // false
console.log(isEmpty(["", {}])) // false

But if this is the only use case you care about then you can also use Array.some:

let isEmpty = a => !a.some(x => x !== '')
// OR let isEmpty = a => !a.some(x => x.toString().length > 0)
console.log(isEmpty(["", "", "", "", "", ""]))
console.log(isEmpty(["", "", "1", "", "", ""]))

You could also use Array.reduce:

let isEmpty = a => !a.reduce((r,c) => `${r}${c}`).length
console.log(isEmpty(["", "", "", "", "", ""]))
console.log(isEmpty(["", "", "1", "", "", ""]))

Array.filter:

let isEmpty = a => !a.filter(x => x.toString().length).length
console.log(isEmpty(["", "", "", "", "", ""]))
console.log(isEmpty(["", "", "1", "", "", ""]))

This will only be valid check against your current input however. Not against cases with arrays, object literals etc as part of your input array.


If you are using lodash this could be (via _.every and _.isEmpty):

let isArrayEmpty = a => _.every(a, _.isEmpty)
console.log(isArrayEmpty(["", "", "", "", "", ""]))
console.log(isArrayEmpty(["", "", "1", "", "", ""]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

or also via _.compact which also removes falsey values:

let isArrayEmpty = a => !_.compact(a).length
console.log(isArrayEmpty(["", "", "", "", "", ""]))
console.log(isArrayEmpty(["", "", "1", "", "", ""]))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

answered May 27, 2019 at 5:38

Comments

1

const isEmpty=arr=>{
 return arr.filter(it=>it.length>0).length ==0
}
arr1 = ["",""]
arr2 = ["1",""]
console.log(isEmpty(arr1))
console.log(isEmpty(arr2))

answered May 27, 2019 at 4:36

Comments

1
var arr1 = ["", "", "", "1", "", ""];
var arr2 = ["", "", "", "", "", ""];
function checkArrValEmpty(arr) {
 let count = 0;
 for(let i = 0; i < arr.length; i++) {
 if(arr[i].trim().length === 0) {`enter code here`
 count++;
 }
 }
 return count === arr.length
}
console.log(checkArrValEmpty(arr2));
console.log(checkArrValEmpty(arr1));
answered May 27, 2019 at 5:01

Comments

1

function isEmpty (arr){
 return arr.every(e => e === "")
};
let a = ["", "", "", "1", "", ""];
let b = ["", "", "", "", "", ""] 
console.log(isEmpty(a));
console.log(isEmpty(b));

answered May 27, 2019 at 6:17

Comments

1

I implemented small function for checking empty array which will work in all browsers.

var list=["1", [], "", null, undefined, ""];
var listE=["", "", "","", "", "", ""];
var listE1=["", [], "", null, undefined, ""];
function isArrayEmpty(arr){
return arr.toString().replace(/,/g, '')=="";
}
alert(isArrayEmpty(list))
alert(isArrayEmpty(listE))
alert(isArrayEmpty(listE1))

answered May 27, 2019 at 6:25

Comments

1

1) Maintain the size of array as length
2) Find the no. of empty values in the array and store it in say an variable (EmptyValues)
3) Check if (EmptyValue== length) { Give Alert whatever you want}

function checkBlankvalues(arr){
var emptyValues=0,length=arr.length;
//for finding empty vales in array till now
for(var i=0;i<arr.length;i++)
{ 
 if(arr[i]="")
 {
 emptyValues++;
 }
}
if(length==emptyValue)
{
 //alert("array is blank");
}
}
answered May 31, 2019 at 6:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.