This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Checking if object is Array or not in Javascript ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Checking if object is Array or not in Javascript ?" Watch "Checking if object is Array or not in Javascript ?" New topic
Author

Checking if object is Array or not in Javascript ?

Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 145

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

Joined: Jan 10, 2002
Posts: 60817
    
  65

The jQuery.isArray() method is the best way to check.

Another approach: x.constructor==Array

The typeof operator is of no help for this.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Karn Kumar
Ranch Hand

Joined: Aug 06, 2009
Posts: 145

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

Joined: Jan 10, 2002
Posts: 60817
    
  65

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

Joined: Aug 06, 2009
Posts: 145

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

Joined: Jan 10, 2002
Posts: 60817
    
  65

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Checking if object is Array or not in Javascript ?
 
Similar Threads
private classes with in servlets
What is meant by "Object... k"
Is there a concept of Map in javascript
Using Observable/Observer
Arrays In Question