/    Sign up×
Community /Pin to ProfileBookmark

Javascript Hashes. Confusion.

Hello Everyone!

I am new to JS and esp to Object Oriented Javascript. I am having to deal with some complicated JS code that involves extensive use of hashes. The idea is that the data is being accessed from some Database and stored in these hashes. Although I know that a hash is essentially a key-value pair. I am confused with the following declaration and usage of hashes.

  • 1.

    temp = aHash[“A”][ some[i] ];
    returnData[temp];

  • 2.

    var b = hash[key][“B”];

  • 3.

    cHash = d[ some[i] ];
    if(cHash[“A”])
    {
    doSomething….
    }

  • Can someone please explain me above syntaxes with examples? Any help will be greatly appreciated.

    to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @rnd_meAug 13.2008 — the brackets , [], are lexically interchangeable with dots, ., to refer to variable paths in multi-level, or tree-like, javascript objects.

    eg:
    [CODE]
    hash = { a:1, b:2, c:3 }
    alert( hash.a === hash["a"] ) //true[/CODE]


    the advantage of using brackets is that an expression, rather than a string keyname, can be used to build the path.

    the expression can come from anywhere: another object look-up, an array element, a dom input tag's .value attrib, an anonymous function, etc. the patterns you show presumably use a simple, flat object name "some" to hold key/value pairs, the value of which is the key to sub-branches of the larger objects aHash, cHash, etc.
    ×

    Success!

    Help @labheshr spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.21,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...