Checking to see if a property exists within an object. - Javascript
This is a discussion on Checking to see if a property exists within an object. - Javascript ; Hi everyone.
I am passing data through JSON objects between the server and client
via Ajax. I would like to know if it's possible to check for the
existence of a property within that object without using try and catch
...
-
Checking to see if a property exists within an object.
Hi everyone.
I am passing data through JSON objects between the server and client
via Ajax. I would like to know if it's possible to check for the
existence of a property within that object without using try and catch
blocks?
I was hoping that there might be something else that will just return
null if the property doesn't exist. If there's not, then it's not a
problem. I can easily make a function that will check to see if the
object exists, and pass back null if it doesn't. I just wanted to save
on unnecessary code if I could.
Many thanks in advance.
Daz.
-
Re: Checking to see if a property exists within an object.
Daz wrote:
> I am passing data through JSON objects between the server and client
> via Ajax. I would like to know if it's possible to check for the
> existence of a property within that object without using try and catch
> blocks?
"propertyName" in object
is one way. Or
typeof object.propertyName != 'undefined'
--
Martin Honnen
http://JavaScript.FAQTs.com/
-
Re: Checking to see if a property exists within an object.
On May 4, 11:15 am, Daz <cutenfu...@> wrote:
> Hi everyone.
>
> I am passing data through JSON objects between the server and client
> via Ajax. I would like to know if it's possible to check for the
> existence of a property within that object without using try and catch
> blocks?
Yes. Use in operator :
Boolean='property' in object
Example:
obj={myprop1:222,myprop2}
if('myprop3' in obj) // < false
alert("Yes, myprop3 exists in object obj")
var Var="myprop2"
if(Var in obj) // < true
alert("Yes, myprop2 exists in object obj")
-
Re: Checking to see if a property exists within an object.
On May 4, 6:21 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> Daz wrote:
> > I am passing data through JSON objects between the server and client
> > via Ajax. I would like to know if it's possible to check for the
> > existence of a property within that object without using try and catch
> > blocks?
>
> "propertyName" in object
> is one way. Or
> typeof object.propertyName != 'undefined'
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/
Wonderful. Thanks a lot Martin.
Similar Threads
-
By Application Development in forum Fortran
Replies: 12
Last Post: 12-07-2007, 09:02 AM
-
By Application Development in forum PHP
Replies: 2
Last Post: 09-17-2007, 11:17 AM
-
By Application Development in forum ADO DAO RDO RDS
Replies: 1
Last Post: 09-02-2007, 10:52 AM
-
By Application Development in forum DOTNET
Replies: 1
Last Post: 07-27-2007, 01:11 AM
-
By Application Development in forum Javascript
Replies: 5
Last Post: 07-14-2007, 09:52 AM