1

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

  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().

I've run out of things to try so any help would be greatly received!

1
  • consider using a conditional Array#push instead of assigning with the bracket operator and an index. i.e. if (val !== undefined) {myArr.push(val)} Commented Aug 18, 2018 at 20:08

2 Answers 2

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:

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);

Sign up to request clarification or add additional context in comments.

6 Comments

Ah. Think I misread the output of the logger. So am I right in saying it would actually be , , ie with a space?
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 " ".
spare? or sparse?
Ah. OK. Well your code did the trick nicely. Thanks. I didn't realise you could use keys with arrays?
@ChrisBarrett everything in JavaScript is an object. Object.keys() will thusly work (there are perhaps some fun exceptions regarding null and undefined).
|
0

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);

4 Comments

No arrows in Apps Script, unfortunately. And shouldn't you use forEach instead of 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?
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 Ok, in this case is definitely better forEach, thank you for the enlightenment.

Your Answer

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.