Check for empty element in array
Check for empty element in array
Similar to something I posted recently but not the same :-)
I'm trying to test in GAS if an array contains any empty values. includes()
doesn't seem to be supported so I've been trying to use index but failing miserably. My test code is below with the empty element before 7
includes()
var e = [1,2,3,4,2,4,,7];
var x = 0;
if (e.indexOf() ==-1)
var x = 'No blanks';
But no matter what I try it always passes. I've already tried indexOf("")
, indexOf('')
& indexOf()
.
indexOf("")
indexOf('')
indexOf()
I've run out of things to try so any help would be greatly received!
Array#push
if (val !== undefined) myArr.push(val)
2 Answers
2
What you have there is a spare array. The missing element there isn't the empty string, it's just not there at all. Because the missing element there isn't enumerable either, it won't show up when you use array methods like indexOf
. You might check to see whether the number of keys is equal to the length
of the array:
indexOf
length
var e = [1,2,3,4,2,4,,7];
var x = Object.keys(e).length === e.length
? 'OK!'
: 'Blank element detected';
console.log(x);
// constrast with a normal array:
e = [1,2,3,4,2,4,7];
x = Object.keys(e).length === e.length
? 'OK!'
: 'Blank element detected';
console.log(x);
Ah. Think I misread the output of the logger. So am I right in saying it would actually be
, ,
ie with a space?– Chris Barrett
2 days ago
, ,
No, nothing is there at all - it's not a space, nor is it the empty string. For it to be a space, it would have to be enclosed in string delimiters, like
' '
or " "
.– CertainPerformance
2 days ago
' '
" "
spare? or sparse?
– tehhowch
2 days ago
Ah. OK. Well your code did the trick nicely. Thanks. I didn't realise you could use keys with arrays?
– Chris Barrett
2 days ago
@ChrisBarrett everything in JavaScript is an object.
Object.keys()
will thusly work (there are perhaps some fun exceptions regarding null
and undefined
).– tehhowch
2 days ago
Object.keys()
null
undefined
To find the keys of all empty elements if there any:
var e = [1,,3,4,2,,7];
var o = Object.keys(e);
m = ;
o.forEach(function(e,i)
if(i>0 && e-o[i-1]>1)
m.push(e-1)
)
console.log(m);
console.log(m.length);
No arrows in Apps Script, unfortunately. And shouldn't you use
forEach
instead of map
?– tehhowch
2 days ago
forEach
map
@tehhowch I didn't know that about Apps Script. and forEach.. it's the same in this case I guess I'm iterating the keys, why do you think is better forEach?
– Emeeus
2 days ago
Consider a large array -
map
returns an equivalent-size array. In your case it is discarded immediately, but it is still created (and every index is undefined
). forEach
invokes a callback just like map
, but there is no return value.– tehhowch
2 days ago
map
undefined
forEach
map
@tehhowch Ok, in this case is definitely better
forEach
, thank you for the enlightenment.– Emeeus
2 days ago
forEach
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
consider using a conditional
Array#push
instead of assigning with the bracket operator and an index. i.e.if (val !== undefined) myArr.push(val)
– tehhowch
2 days ago