Javascript join Array

Source: Internet
Author: User

Address: http://wyllife.blog.163.com/blog/static/4116390120115210038676/

1. Associate an array (hash array)
Standard JavaScript contains arrays that support hash Association. After searching for information and testing, the operations on the hash Association array contained in standard JavaScript are as follows:

1. Hash join array Definition

// Define an empty array
Myhash = {}

// Directly define an array
Myhash = {"key1": "val1", "key2": "val2 ″}

// Define an array with Array
Myhash = new array ();
Myhash ["key1"] = "val1 ″;
Myhash ["key2"] = "val2 ″;

2. Add a key value to the hash join Array

// Add a new key newkey with the key value newval
Myhash ["newkey"] = "newval ";

3. Deletes an existing key value from the hash associated array.

// Delete a key newkey. At the same time, the newval corresponding to the key value disappears.
Delete myhash ["newkey"];

4. Traverse the hash associated array

// Traverse the entire hash Array
For (key in myhash ){
Val = myhash [Key];
}

5. Simple use example of hash join Array

// Redirection script
<SCRIPT type = "text/JavaScript">
Urlhash = {"Yahoo": "www.yahoo.cn",
"Baidu": "www.baidu.com",
"Google": "www.google.cn "};

// Interactive use example
Userinfo = prompt ("enter the search engine you most want to go to: (Yahoo | Baidu | Google)", "Yahoo ");
Document. write ("your choice:" + userinfo + ", <a href = http: //" + geturl (userinfo) + "target = _ blank>" + "Click here to enter" + "</a>" + userinfo + ". ");

// geturl
// If the parameter is not defined, www.yahoo.cn is returned by default.
// @ Param choice select the name
// @ return URL actual URL
function geturl (choice) {
url = urlhash [choice];
If (typeof (urlhash [choice]) =" undefined ")
url =" www.yahoo.cn ";
return URL;
}

// Obtain all the keys in the hash list
// @ Param Hash hash Array
// @ Return keys key name data
Function array_keys (hash ){
Keys = [];
For (key in hash)
Keys. Push (key );
Return keys;
}
</SCRIPT>

2. Normal Array

Syntax for creating an array object:
 
New array (); new array (Size); New array (Element0,Element1,...,Elementn);

End

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.