Jump to content


Photo

Distinguish between Array and Object


  • Please log in to reply
2 replies to this topic

#1 ShadowMage

ShadowMage

    Faal Vokundovah

  • Members
  • PipPipPipPipPip
  • 3,395 posts
  • Gender:Male
  • Location:America's dairyland
  • Interests:I dabble in a little bit of everything. Gaming, programming, web development, fishing, paintballing, woodworking, farm equipment, reading, nature......
  • Languages:(X)HTML, CSS, JavaScript, PHP (though I still have much to learn)

Posted 29 March 2012 - 04:50 PM

I have some JSON that will either return a fully populated object:
{a: 0, b: 1, c: 2}
or an empty array:
[]

What I'd like to know is, what is the easiest way to distinguish between the two? I know I could do something like:

if (obj.a) {

or

if (obj.length !== undefined) {

but these have drawbacks such that:
- if the array object happens to have property a, the first if will fail
- if the object happens to have a length property, the second one will fail

I don't anticipate either of those situations ever happening in this situation because all of the properties are hand written, not dynamically generated. However, I want to know for future reference if there is a better way.

Thanks.
In programming, failure is not an option. It's an inevitability...
Mosher's Law of Software Engineering: "Don't worry if it doesn't work right. If everything did, you'd be out of a job."

Call upon the FireBug and its swarm!
Darkness Falls - W3Schools Forum skin for Stylish

#2 thescientist

thescientist

    Likes gettin' it on!

  • Moderator
  • PipPipPipPipPipPipPip
  • 8,276 posts
  • Gender:Male
  • Location:Rhode Island, USA
  • Interests:Music, programming, computers, web design, business, books, and politics.
  • Languages:HTML/CSS, JS, PHP/MySQL, XML, bash, SVN/Git, Java, Python

Posted 29 March 2012 - 05:26 PM

http://stackoverflow...ash-or-an-array

seems like an option.

Edited by thescientist, 29 March 2012 - 05:40 PM.

myTunes: www.analogstudios.net

my {...} code {...} works in all the decent browsers. This of course means that it does not work in IE.


#3 ShadowMage

ShadowMage

    Faal Vokundovah

  • Members
  • PipPipPipPipPip
  • 3,395 posts
  • Gender:Male
  • Location:America's dairyland
  • Interests:I dabble in a little bit of everything. Gaming, programming, web development, fishing, paintballing, woodworking, farm equipment, reading, nature......
  • Languages:(X)HTML, CSS, JavaScript, PHP (though I still have much to learn)

Posted 29 March 2012 - 06:30 PM

Yeah! I never thought of constructor :facepalm: and didn't even know instanceof existed. I use typeof quite often but that won't work in this situation since arrays are objects. The constructor property and instanceof both work very well.

Thanks for the link! :good:
In programming, failure is not an option. It's an inevitability...
Mosher's Law of Software Engineering: "Don't worry if it doesn't work right. If everything did, you'd be out of a job."

Call upon the FireBug and its swarm!
Darkness Falls - W3Schools Forum skin for Stylish




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users