isArray() method. This method returns true if the Object passed as a parameter is an array. It also checks for the case if the array is undefined or null. The array can be checked if it is empty by using the array.
The first condition covers truthy, which has both null and undefined. Second condition checks for an empty array. if(arrayName && arrayName. length > 0){ //do something. }
There's no such thing as an "empty array" or an "empty element" in C. The array always holds a fixed pre-determined number of elements and each element always holds some value. The only way to introduce the concept of an "empty" element is to implement it yourself.
As long as your selector is actually working, I see nothing wrong with your code that checks the length of the array. That should do what you want. There are a lot of ways to clean up your code to be simpler and more readable. Here's a cleaned up version with notes about what I cleaned up.
var album_text = [];
$("input[name='album_text[]']").each(function() {
var value = $(this).val();
if (value) {
album_text.push(value);
}
});
if (album_text.length === 0) {
$('#error_message').html("Error");
}
else {
//send data
}
Some notes on what you were doing and what I changed.
$(this)
is always a valid jQuery object so there's no reason to ever check if ($(this))
. It may not have any DOM objects inside it, but you can check that with $(this).length
if you need to, but that is not necessary here because the .each()
loop wouldn't run if there were no items so $(this)
inside your .each()
loop will always be something.[]
rather than new Array()
.if (value)
when value is expected to be a string will both protect from value == null
, value == undefined
and value == ""
so you don't have to do if (value && (value != ""))
. You can just do: if (value)
to check for all three empty conditions.if (album_text.length === 0)
will tell you if the array is empty as long as it is a valid, initialized array (which it is here).What are you trying to do with this selector $("input[name='album_text[]']")
?
User JQuery is EmptyObject to check whether array is contains elements or not.
var testArray=[1,2,3,4,5];
var testArray1=[];
console.log(jQuery.isEmptyObject(testArray)); //false
console.log(jQuery.isEmptyObject(testArray1)); //true
I think it is dangerous to use $.isEmptyObject from jquery to check whether the array is empty, as @jesenko mentioned. I just met that problem.
In the isEmptyObject doc, it mentions:
The argument should always be a plain JavaScript Object
which you can determine by $.isPlainObject
. The return of $.isPlainObject([])
is false.
You should check for ''
(empty string) before pushing into your array. Your array has elements that are empty strings. Then your album_text.length === 0
will work just fine.
/*
Basic Checking with undefined array for Jquery Array
*/
if (typeof myArray !== 'undefined' && myArray.length > 0) {
console.log('myArray is not empty.');
}else{
console.log('myArray is empty.');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With