0

I need to check if an array has any null value. If it does return false otherwise true.

How do I turn this

checkArray(array){
 for (var i = 0; i < array.length; i++) {
  if (array[i] === null || array[i] === '')
    return false;
 }
 return true;
}

into a working example with map and fat arrows?

I've tried this

checkArray(array){
  array.map(function(item) {
   if (item === null || item === '') {
     return false;
   } else {
     return true;
   }
  return true;
 })
}

But it always returns true. Why? Is it because map creates a new array?

5
  • Are looking for Array#filter ? Commented Apr 11, 2016 at 11:26
  • I don't think so, I'll clarify in the question Commented Apr 11, 2016 at 11:27
  • it always returns undefined because you return nothing from checkArray Commented Apr 11, 2016 at 11:27
  • @vp_arth sorry I missed a bit. Check my edited code, but even then it always returns true Commented Apr 11, 2016 at 11:30
  • array.map should return [true, false, false, true] like array in your case. Commented Apr 11, 2016 at 11:34

5 Answers 5

4

It's the good fit for the underutilised Array.prototype.some():

array.some(v => v === '' || v === null);

What it does is returns a boolean value that is true if the predicate is true for any of the array values, and false otherwise.

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

Comments

4

Another option is to use includes() method of the Array:

var isWithoutEmptyElements = !array.includes(null) && !array.includes('');

5 Comments

your var should be named hasNotEmpty, isn't it?
@DmitriPavlutin Error in logic? It should be arr.includes(null) || arr.includes(''). I need to check if an array has any null value.
@Tushar The method should return false if there is at least one null or empty string. The logic is correct.
Why not isThisArrayDoesNotContainAnyNullOrEmptyStringElements? =)
@vp_arth Next time :D
1

The problem with the map variant is that nothing is returned from function.

A better way is to use Array#find

array.find(el => el === null || el === '');

var arr = [1, 23, 4, 'something', null, 3235, true];
var found = arr.find(el => el === null || el === '');
console.log(found); // null

Comments

1

In your code, you are using a .map(), it creates a new array with the values returned by the callback, but you are not returning any value from checkArray that is why you are getting undefined.

You want to make sure that none of the item is null or '' so use every()

checkArray(array) {
  return array.every((item) => item !== undefined && item !== '');
}

Comments

1

You're looking for reduce.

var found = array.reduce(function(acc, value) {
    if(acc){
        return true;
    }

    if (value === null || value === ''){
       return true;
    }

    return false;
}

5 Comments

some much better
Agreed, didnt know ES6 had some.
@LukaJacobowitz it actually was there in ES5 as well
Even more embarrassing :D
@LukaJacobowitz yep, that's why I called it "underutilised" - because for some magical reason people tend to not knowing about it at all :-) (and it's fine though)

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.