one line "hash"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Thies

    one line "hash"

    I have this:

    var hash=new Array();
    hash['a']='some_value';
    hash['b']='some other value';

    Whar do you call that in javascript?

    I know it's possible to define this in one line, but I've forgotten how.
    Something like:

    var hash=new Array('a':'some value','b':'som e other value') ???

    Jeff


  • ZER0

    #2
    Re: one line "hash&quot ;

    On Thu, 10 Jun 2004 04:59:14 GMT, Jeff Thies wrote:
    [color=blue]
    > var hash=new Array();
    > hash['a']='some_value';
    > hash['b']='some other value';[/color]
    [color=blue]
    > Whar do you call that in javascript?[/color]

    Array. With two new custom properties, called "a" and "b":

    alert(hash.a);
    alert(hash.b);
    [color=blue]
    > I know it's possible to define this in one line, but I've forgotten how.
    > Something like:[/color]
    [color=blue]
    > var hash=new Array('a':'some value','b':'som e other value') ???[/color]

    You mean:

    var hash={a:"some value",b:"some other value");

    This is not an Array. This is an Object. And this syntax is called "Object
    Initializer".

    The previous code is equals to:

    var hash=new Object();
    hash.a="some value";
    hash.b="some other value";

    --
    ZER0://coder.gfxer.web-designer/

    ~ "When you have eliminated the impossible, whatever remains,
    however improbable, must be the truth." (S.H.)

    Comment

    • Douglas Crockford

      #3
      Re: one line "hash&quot ;

      > I have this:[color=blue]
      >
      > var hash=new Array();
      > hash['a']='some_value';
      > hash['b']='some other value';
      >
      > Whar do you call that in javascript?
      >
      > I know it's possible to define this in one line, but I've forgotten how.
      > Something like:
      >
      > var hash=new Array('a':'some value','b':'som e other value') ???[/color]

      You should be using an object for this application, not an array.

      var hash = {a: "some_value ", b: "some other value"};

      And don't call it a hash. It makes you look like a Perl Boob.

      See http://www.JSON.org

      Comment

      Working...