[jQuery] Re: Accessing a JSON property from an unknown variable?

2009-02-14 Thread seasoup

It's not really an associative array.  It is the property of an
object, there are two notations for getting at an objects properties:

foo.bar

and

foo['bar']

though you could even:

eval('foo.' + fooProp);

foo.bar is the preferred method, but if the property name has a space
in it or needs to be evaluated the second method is used.  It looks
like an associative array but it will not have any of the array
methods, nor a .length.  This gets really tricky when foo is an array:

var foo = [];
foo['bar'] = 'value';

has the array methods, but has a length of 0.  but if you

foo[1234] = 'value';

then you have an array of length 1235 and the first 1234 value are
undefined.

Josh Powell


On Feb 13, 4:36 pm, Nic Luciano adaptive...@gmail.com wrote:
 Oh wow, I didn't realize JSON would act like an associative array in that
 way...

 Thanks guys!

 On Fri, Feb 13, 2009 at 7:10 PM, Josh Nathanson 
 joshnathan...@gmail.comwrote:



  foo[fooProp] // returns barVal

  Is that what you mean?

  -- Josh

  -Original Message-
  From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
  Behalf Of Nic
  Sent: Friday, February 13, 2009 4:06 PM
  To: jQuery (English)
  Subject: [jQuery] Accessing a JSON property from an unknown variable?

  For instance,

  var foo = {
  bar: barVal,
  baz: bazVal
  }

  var fooProp = bar;

  How can I access barVal through fooProp?

  I know this isn't exactly jQuery group discussion but I figured since
  it was part of a jQuery system I could get away with it. Thanks!


[jQuery] Re: Accessing a JSON property from an unknown variable?

2009-02-13 Thread James

alert( foo[fooProp] );

On Feb 13, 2:05 pm, Nic adaptive...@gmail.com wrote:
 For instance,

 var foo = {
 bar: barVal,
 baz: bazVal

 }

 var fooProp = bar;

 How can I access barVal through fooProp?

 I know this isn't exactly jQuery group discussion but I figured since
 it was part of a jQuery system I could get away with it. Thanks!


[jQuery] Re: Accessing a JSON property from an unknown variable?

2009-02-13 Thread Josh Nathanson

foo[fooProp] // returns barVal

Is that what you mean?

-- Josh



-Original Message-
From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
Behalf Of Nic
Sent: Friday, February 13, 2009 4:06 PM
To: jQuery (English)
Subject: [jQuery] Accessing a JSON property from an unknown variable?


For instance,

var foo = {
bar: barVal,
baz: bazVal
}

var fooProp = bar;

How can I access barVal through fooProp?

I know this isn't exactly jQuery group discussion but I figured since
it was part of a jQuery system I could get away with it. Thanks!



[jQuery] Re: Accessing a JSON property from an unknown variable?

2009-02-13 Thread Nic Luciano
Oh wow, I didn't realize JSON would act like an associative array in that
way...

Thanks guys!

On Fri, Feb 13, 2009 at 7:10 PM, Josh Nathanson joshnathan...@gmail.comwrote:


 foo[fooProp] // returns barVal

 Is that what you mean?

 -- Josh



 -Original Message-
 From: jquery-en@googlegroups.com [mailto:jquery...@googlegroups.com] On
 Behalf Of Nic
 Sent: Friday, February 13, 2009 4:06 PM
 To: jQuery (English)
 Subject: [jQuery] Accessing a JSON property from an unknown variable?


 For instance,

 var foo = {
 bar: barVal,
 baz: bazVal
 }

 var fooProp = bar;

 How can I access barVal through fooProp?

 I know this isn't exactly jQuery group discussion but I figured since
 it was part of a jQuery system I could get away with it. Thanks!