The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Empty array
Discuss Empty array in the JavaScript Development forum on Dev Shed. Empty array JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 15th, 2008, 02:22 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
Empty array
Hello,
I have this set of two functions which replicate the problem i have.
javascript Code:
Original
- javascript Code |
|
|
|
function check(){ var a = arr(); alert('number 1 is '+a.num1); alert('number 2 is '+a.num2); alert('a is '+a); alert('length of a is '+a.length); return true; } function arr(){ var ar = new Array(); ar['num1'] = 19.97; ar['num2'] = 19.98; return ar; }
a.num1 and a.num2 show up correctly but the a.length returns 0. Is that expected behaviour or am I missing something?
Thank you.
__________________
Why do we always seek someone, something or some thought? Are we afraid of ourselves?
|

July 15th, 2008, 05:04 AM
|
 |
Contributing User
|
|
Join Date: Jun 2004
Location: Surrey, UK
|
|
|
You're not actually setting any elements in the array... by using arr["name"] you are actually setting a property called name on the object rather than adding an element to it and therefore not updating the length.
__________________
Scott Perham - MCPD
That URL too long? Why not URL IT!
|

July 15th, 2008, 05:32 AM
|
 |
Contributing User
|
|
Join Date: Aug 2005
Location: Bucharest ROMANIA
|
|
Quote: | Originally Posted by srisa Is that expected behaviour or am I missing something?
|
It is an expected behavior, yes. You have not build an ordered array, you have build an associative array. An associative array has no length.
Ordered array (vector, list, sequence):
Code:
var myArray=[];
myArray[0]='John';
myArray[1]='Bull';
Associative array (object, record, struct, dictionary, hash table, keyed list):
Code:
var myObject={};
myObject['firstname']='John';
myObject['lastname']='Bull';
|

July 15th, 2008, 06:20 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
Then, how do I check that the associative array is empty or not empty?
|

July 15th, 2008, 06:30 AM
|
 |
Contributing User
|
|
Join Date: Jun 2004
Location: Surrey, UK
|
|
You could do something like:
Code:
var length = 0;
for (var i in a)
length++;
Of course this would only work if the object "a" was created using {} rather than a new array...
|

July 15th, 2008, 06:55 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
Is it acceptable to use if (!a.num1) to check that a is empty? It is working, but I am not sure it is a good idea.
|

July 15th, 2008, 06:58 AM
|
 |
Contributing User
|
|
Join Date: Jun 2004
Location: Surrey, UK
|
|
Thats fine, unless num1 is a boolean property  it might be better to check using:
Code:
if (typeof a["num1"] != "undefined")
...
|

July 15th, 2008, 07:16 AM
|
|
Contributing User
|
|
Join Date: May 2006
Location: I'm sneaking up behind you.
|
|
|
num1 won't be a boolean. It will be a number or empty. But still, your code should be used and I will use it. Thanks a lot.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|