I set an array which have key in string Like below in my code :
var a = new Array();
a[1] = new Array();
a[1]['mystring'] = new Array();
a[1]['mystring'] = 'test';
if(isNullValues(a[1])) {
alert("Array Empty.");
}
function isNullValues(data) {
if (data == undefined || data == null || data.length == 0 || data == "") {
return true;
} else {
return false;
}
}
It alerts me Array Empty string. But it shouldn’t return this?
There are no associative arrays in JavaScript. What you are doing is adding a property “mystring” to the array in a[1]. Hence, the internal counter “length” does not get incremented and
a[1].length == 0is true, therefore “isNullValues()” returns true.You can “dirty fix” this by using a plain object: