0

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?

6
  • What happens if you use the 0 as first index instead of 1? Commented Jan 8, 2013 at 12:08
  • 1
    I dont get the alert.. It works for me (verified with an else{console.log(1)}), and it does log 1. Commented Jan 8, 2013 at 12:09
  • ^^^^^ Me neither - jsfiddle.net/VqXsH. By the way, you should be using an object instead of the inner array: ` a[1] = {}`. Commented Jan 8, 2013 at 12:10
  • I try your code and it works for me. (a.length) evaluates to '2' and (a.toString) evaluates to ',' So, it's strange you having that problem Commented Jan 8, 2013 at 12:13
  • @EricG sry i updated my code plz check.. Commented Jan 8, 2013 at 12:13

4 Answers 4

2

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 == 0 is true, therefore "isNullValues()" returns true.

You can "dirty fix" this by using a plain object:

var a = new Array();
a[1] = {};
a[1]['mystring'] = 'test';
Sign up to request clarification or add additional context in comments.

4 Comments

so there is no way to check this?
You have a nice in-depth info about this issue on blog.xkoder.com/2008/07/10/…
You can count the properties of this array instance, but that's not actually a good solution. If you need to simulate an associative array use a plain object as demonstrated above.
changed mystring key to 0 instead of use object
0

Try this one:

 var a = new Array();
 a[1] = new Object();
 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;
     } 
  }

I changed new Array() in new Object().

You can also write a as var a = [undefined, {mystring: 'test'}];

Comments

0

The code shown should indeed display the alarm because:

a[1]['mystring'] = new Array();

Simply adds a new property called mystring to the array contained in a[1] meaning that:

a[1].length

... is still 0, since no elements are being added to the actual array. And with 0 elements in the array, your isNullValues function returns true because of the check:

data.length == 0

... causing the alert to be displayed.

Comments

0

As it's been said, your first problem is using an array instead of an object. Now, if you want to check if an object is empty (has no keys), you have to loop its keys:

function isObjectEmpty(obj)
    for(var p in obj) {
        if(obj.hasOwnProperty(p)) return false;
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.