• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Checking if object is Array or not in Javascript ?

 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

I came across situation that i need to check if object is Array or not in native javascript .

If anyone has came across such situation . Please suggest .

Thanks
- Chetan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The jQuery.isArray() method is the best way to check.

Another approach: x.constructor==Array

The typeof operator is of no help for this.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear,

Yeah typeof operator is of no help.

I checked x.constructor==Array in the IE ,where x is the object (in my case document.formname.elementname)

document.test.HightLimit.constructor---> shows undefined .

Is there is any other method to detect .


Thanks in advance.

- Chetan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to give enough information to answer questions without having to be asked for it.

What is document.test.HightLimit? Without knowing that, nothing else can be said.
 
Karn Kumar
Ranch Hand
Posts: 153
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed some of the things..


document.test.HightLimit

test ---> is my form name in the JSP

HighLimit--> is the name of the entry field in the Jsp which can be single time or multiple time in table .

(If it is multiple times then it gives document.form.HighLimit.length as proper value say 3 when field is 3 time appearing in the page, but if it is single time then document.form.HighLimit.length gives undefined . This is the only difference i found , By checking length i can identify as it is Array or not, but i don't know if it is correct approach. )


- Chetan
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an antiquated and error-prone way of addressing elements. Join the 21st century and assign an id to the element and use document.getElementById().

In any case, what makes you feel that a text field can be an array?

You'll need to back up and post what you are really trying to accomplish. Because whatever it is, testing for an array isn't the answer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic