0

So I have an array that I created but I would like to execute an action if one or more variables are empty in my array.

jQuery(document).ready( function($) {

        var fields = ["", "two", "three", "four", "five"];


        if (fields.length === 0) {
            alert('One or more variables are empty!');
        }

        else {
          alert('All variables are here!');
        }    


   });    

I know this code above is wrong because it will always launch the latter.

So basically

var fields = ["", "two", "three", "four", "five"];

Should return One or more variables are empty!

And this:

 var fields = ["one", "two", "three", "four", "five"];

Should return All variables are here!

0

3 Answers 3

2

One way to do it would be to use Array.prototype.every(). One thing that's nice about every() is that if a single element returns falsey, it will short-circuit the array iteration, meaning that it will not continue to unnecessarily process the remaining elements in the array.

var fields = ["", "two", "three", "four", "five"]; // invalid array
var fields2 = ["one", "two", "three", "four", "five"]; // valid array

function validateFields (fields) {
  var everyFieldIsValid = fields.every(function (field) {
    // ensure there are no gaps in the array and that each element is not
    // undefined, null, or equal to empty string ("")
    return field != null && field !== "";
  }); 
  
  if (everyFieldIsValid) {
    // success logic goes here - you can console.log, alert, return true/false, etc.
    return "All variables are here!";
  }
  else {
    // failure logic goes here - you can console.log, alert, return true/false, etc.
    return "One or more variables are empty!";
  }
}

console.log(validateFields(fields));
console.log(validateFields(fields2));

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

3 Comments

A very simple solution. I never thought of this. Will try it now!
Worked perfectly! Sorry for the delayed response.
@Mariton Great! Glad to hear it =)
2

You can check if every array element conforms to your condition using a function Array.prototype.every:

var fields = ["", "two", "three", "four", "five"];

var res = fields.every(f => f != null && f.length) ? 'all here' : 'some are empty';

console.log(res)

every(f => f != null && f.length) checks if every element is defined, not equal to null and has length > 0. res is assigned an answer depending on this condition (ternary operator is used instead of if to make the code more clear).

1 Comment

"ternary operator is used instead of if to make the code more clear"... said no one ever...
0

Do this:

 fields.filter(function(object){
        return object.trim().length > 0;
    });

this will delete all the empty fields. Simply add conditionnal if you want to know is there is a empty field!

1 Comment

This code makes a lot of assumptions.. 1. Assuming every element in the array is a string, and there are no "holes" in the array, 2. Assuming the OP wants to remove the empty elements I am pretty sure neither one of those assumptions are accurate, but you should ask the OP for clarification.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.