JS Associative array

Source: Internet
Author: User
Tags array definition

Standard JavaScript is included in the support hash associative array, after finding data and testing, about standard JavaScript contains hash associative array operation memo as follows

1. Hash associative array definition

Define an empty array Myhash = {}

Directly define the array Myhash = {"Key1″:" Val1″, "Key2″:" Val2″}

Define Array Myhash = new array with array (); myhash["Key1″] =" Val1″; myhash["Key2″] =" Val2″;

2. Add a key value to a hash associative array

Add a new key Newkey, the key value is newval myhash["newkey"] = "newval";

3. Delete Hash associative array already has key value

Delete a key newkey, at the same time, the key value corresponding to the newval also disappeared delete myhash["Newkey"];

4. Traversing hash associative arrays

Traverse the entire hash array for (key in Myhash) {val = Myhash[key];}

5. Example of simple use of hash associative array

Steering script <script type= "Text/javascript" > urlhash = {"Yahoo": "www.yahoo.cn", "Baidu": "Www.baidu.com", "Google": " Www.google.cn "};

Interactive use Example userinfo = prompt ("Please enter your most wanted search engine: (yahoo|baidu|google)", "Yahoo"); document.write ("Your choice:" + UserInfo + ", <a href=http://" + GetURL (userinfo) + "target=_blank>" + "Click here to enter" + "</a& gt; "+ UserInfo +". ”);

GetURL//If the parameter is undefined, the default return www.yahoo.cn URL//@param choice select name//@return URL actual URL function getURL (choice) {URL = urlh Ash[choice]; if (typeof (Urlhash[choice]) = = "undefined") url = "www.yahoo.cn"; return URL; }

Get hash list of all keys//@param hash hash array//@return keys Key name data function Array_keys (hash) {keys = []; for (key in hash) Ke Ys.push (key); return keys; } </script>

JS Associative array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.