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.
-
Possible duplicate of How to watch for array changes?Faisal Rahman Avash– Faisal Rahman Avash2019年05月27日 04:36:08 +00:00Commented May 27, 2019 at 4:36
-
1How is it a duplicate @Faisal?Jack Bashford– Jack Bashford2019年05月27日 04:36:47 +00:00Commented May 27, 2019 at 4:36
-
I think the description is more suited to describe observable arrays.Faisal Rahman Avash– Faisal Rahman Avash2019年05月27日 04:38:19 +00:00Commented May 27, 2019 at 4:38
-
1But it is not be duplicate @Faizal.Jaydeep Mor– Jaydeep Mor2019年05月27日 04:39:27 +00:00Commented May 27, 2019 at 4:39
-
Erm...the above doesn't need to observe anything @FaisalJack Bashford– Jack Bashford2019年05月27日 04:51:52 +00:00Commented May 27, 2019 at 4:51
10 Answers 10
Use every()
:
const allEmpty = arr => arr.every(e => e === "");
console.log(allEmpty(["", "", "", "1", "", ""]));
console.log(allEmpty(["", "", "", "", "", ""]));
2 Comments
==
or ===
for this case, or does it not really matter at all?===
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
)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, ""]
8 Comments
trim()
if there are spaces in the array :)array.join("").replace(/\s/gi,'').length==0
will be improved version of this answer.true
for an input like: ["", [], "", null, undefined, ""]
. If you are strictly looking that all items are empty strings ""
then this won't be good.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:
.some(Boolean)
~40M ops/s (Operation per second)isArrayEmpty()
~36M ops/s (~10% slower).filter(Boolean).length
~9M ops/s (~75% slower).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.
1 Comment
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;}
Comments
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", "", "", ""]))
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>
Comments
const isEmpty=arr=>{
return arr.filter(it=>it.length>0).length ==0
}
arr1 = ["",""]
arr2 = ["1",""]
console.log(isEmpty(arr1))
console.log(isEmpty(arr2))
Comments
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));
Comments
function isEmpty (arr){
return arr.every(e => e === "")
};
let a = ["", "", "", "1", "", ""];
let b = ["", "", "", "", "", ""]
console.log(isEmpty(a));
console.log(isEmpty(b));
Comments
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))
Comments
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");
}
}