Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-12-2012, 09:16 AM   #1
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default check if array is empty/undefined

Hi everyone, quick syntax question.

I'm manually defining some data (hardcoding) in a manner like this:


Code:
myArray = [[a,b,c], [], [1,2,3]];
A 2d array, note the empty array in the middle.
What i want to know, is how to check if one of these arrays is empty, before trying to load data from it. For example, do i use:

Code:
if (myArray[1] != null)
Code:
if (myArray[1] != undefined)
Code:
if (myArray[1].length != 0)
Code:
if (myArray[1] != [])
or something else i haven't thought of? What syntax do i need to successfully exclude that middle array and NOT read it ?
WarKirby is offline   Reply With Quote
Old 05-12-2012, 02:27 PM   #2
laurence485
Senior Member
 
Join Date: May 2010
Location: London, UK
Posts: 100
Send a message via MSN to laurence485
Default

Use .length property, if length is 0 then the array is empty
laurence485 is offline   Reply With Quote
Old 05-12-2012, 03:28 PM   #3
Biodiscus
Immma firing Ninnnnjaaa
 
Join Date: Aug 2011
Posts: 95
Default

Quote:
Originally Posted by laurence485 View Post
Use .length property, if length is 0 then the array is empty
myArray.length. He tried myArray[1].length.
Biodiscus is offline   Reply With Quote
Old 05-12-2012, 10:43 PM   #4
WarKirby
Senior Member
 
Join Date: May 2011
Posts: 283
Default

Quote:
Originally Posted by Biodiscus View Post
myArray.length. He tried myArray[1].length.
no, no. that was deliberate. I'm not checking myarray here, i'm checking the nested array that is in element 1 of myarray (which is empty)
WarKirby is offline   Reply With Quote
Old 05-12-2012, 11:59 PM   #5
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

If you do a trace on the length property of each elements, you should get 3, 0, 3 for those three elements.

Since you are hardcoding it as [], it can never be null or undefined. So, you basically need to check the length of it and see if it is myArray[1].length > 0 and then go on from there.
ActionScript Code:
for (var i:int = 0 ; i < myArray.length ; i++) {     trace(i + " : " + myArray[i].length);     if (myArray[i].length > 0)     {         // This runs only for the first and last elements...         doYourThing(myArray[i]);     } }
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:15 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.