|
-
Mar 25th, 2008, 03:52 PM
#1
Thread Starter
Junior Member
Javascript: test if an object is an array.
Hi! I would like to know if there is an elegant way to test if an object is an array.
Example:
Code:
function ProcessObject(object) {
//Test if it is an array.
if(...) {
for(int i=0; i < object.length; i++) {
//object[i] ...
}
}
else {
//Don't process as an array.
//object.value ...
}
}
In the code above, the function is receiving an object that could be an array or not. The main reason I want to know if it is an array is because I want to use the length property. Do anyone know if there is a property in DOM to know if an object is an array so I can invoke the length property without error?
Thanks a lot!
PS: I make it, in a not elegant way...
Last edited by Tribo; Mar 25th, 2008 at 03:56 PM.
-
Mar 25th, 2008, 04:55 PM
#2
Re: Javascript: test if an object is an array.
Code:
if(object.length != null)
{
//Array!
}
I think...
-
Mar 25th, 2008, 07:48 PM
#3
Thread Starter
Junior Member
Re: Javascript: test if an object is an array.
 Originally Posted by mendhak
Code:
if(object.length != null)
{
//Array!
}
I think...
Perfect! It was so simple, and I was looking for a complicated solution...
Thanks a lot!!!
-
Mar 26th, 2008, 04:02 AM
#4
Re: Javascript: test if an object is an array.
We can provide you with an overly complicated solution too, if that is what you wish.
-
Mar 26th, 2008, 07:52 PM
#5
Re: Javascript: test if an object is an array.
But strings have a length property, too. Are they arrays? Sort of, but probably not the type you mean.
Code:
if (object instanceof Array)
// do something
-
Mar 26th, 2008, 08:06 PM
#6
Thread Starter
Junior Member
Re: Javascript: test if an object is an array.
 Originally Posted by penagate
But strings have a length property, too. Are they arrays? Sort of, but probably not the type you mean.
Code:
if (object instanceof Array)
// do something
I don't remember now, but I think that they are an array of DIVs, and with JavaScript I change the style so I can hide the information or not (it is a kind of Detail that the user could see and by default it isn't shown)
Regards!
-
Mar 26th, 2008, 10:59 PM
#7
Re: Javascript: test if an object is an array.
This is brute but at least it does the job. Tested it too.
Code:
if(obj[0] != null){
alert("array of length " + obj.length);
} else {
alert("not an array");
}
-
Mar 26th, 2008, 11:07 PM
#8
Re: Javascript: test if an object is an array.
What's wrong with instanceof? Does it smell funny or something?
-
Mar 26th, 2008, 11:28 PM
#9
Re: Javascript: test if an object is an array.
Sorry, posting code for future reference on event listeners. eace:
Code:
<html>
<head>
<title>Test Array</title>
<meta http-equiv="Content-Type" content="text/html; charset=shift_jis">
<script type="text/javascript">
var mydivcount = 0;
function checkIfArray(obj){
if(obj[0] != null){
alert("array of length " + obj.length);
} else {
alert("not an array");
}
}
function addDiv(){
var newdiv = document.createElement('div');
newdiv.setAttribute('id', 'mydiv');
newdiv.setAttribute('name', 'mydiv');
// dynamic event listener
if (newdiv.addEventListener){
newdiv.addEventListener('click', removeDiv, false);
} else if (newdiv.attachEvent){
newdiv.attachEvent('onclick', removeDiv);
}
newdiv.appendChild(document.createTextNode("This is another div: " + (++mydivcount)))
document.getElementById('addhere').appendChild(newdiv);
}
function removeDiv(evt){
// target for Mozilla
// srcElement for IE
var el = (evt.target) ? evt.target : evt.srcElement;
document.getElementById('addhere').removeChild(el);
}
</script>
</head>
<body>
<div id="addhere" style="border:1 solid #000000; padding: 10px 0px 10px 0px ">
</div>
<input type="button" value="Add a DIV" onClick="addDiv()">
<input type="button" value="Check Array" onClick="checkIfArray(document.getElementsByName('mydiv'))">
</body>
</html>
-
Mar 26th, 2008, 11:29 PM
#10
Thread Starter
Junior Member
Re: Javascript: test if an object is an array.
 Originally Posted by penagate
What's wrong with instanceof? Does it smell funny or something?
No, I think that it is the most elegant solution (it makes the code more legible). I'm going to try it.
Regards,
-
Mar 26th, 2008, 11:32 PM
#11
Re: Javascript: test if an object is an array.
 Originally Posted by penagate
What's wrong with instanceof? Does it smell funny or something?
I tried instanceof but if the collection is other than an Array type like divs for instance it won't detect it. the other testing is versatile enough I believe.
Code:
if(obj instanceof Array){
alert("array of length " + obj.length);
} else {
alert("not an array");
}
-
Mar 26th, 2008, 11:49 PM
#12
Re: Javascript: test if an object is an array.
DOM node collections are not arrays. In fact there are 'live' collections as well as 'non-live' ones and fragments and so forth. None of these are derived from Array.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|