JavaScript Associative Array Usage summary

Source: Internet
Author: User
Tags array definition arrays cairo hash scalar

This article summarizes the usage summary of JavaScript associative array, and the friend who has to understand the JS associative array can refer to this article.

Just learn JS when it seems to remember that there is no associative array js, write PHP program with associative array habits, write JS when it is awkward, today

In the evening to find some information, said JS is through the object to implement associative array, the general program of data types have the following:

The first type is scalar (scalar), a separate string or number.
The second type is sequence (sequence), a number of related data are tied together in a certain order, usually called list, such as "A,b,c", but in JS this is called array (array, can only access the element PS by subscript: This concept confuses me.
The third type is map (map), a key/value pair (Key/value), also known as hash (hash) or dictionary (dictionary), such as "A:b,c:d".

Since the JS object can be implemented hash table, in JS can be used in the new array () to use associative arrays,

Hash associative array definition

The code is as follows Copy Code

Define an empty array
Myhash = {}

To define an array directly
Myhash = {"Key1″:" Val1″, "Key2″:" Val2″}

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

2. Add a key value to a hash associative array

The code is as follows Copy Code

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

3. Delete Hash associative array already has a key value

The code is as follows Copy Code

Deletes a key newkey, and the corresponding newval of the key value disappears.
Delete myhash["Newkey"];

4. Traversal hash Associative array

The code is as follows Copy Code

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

5. Simple use example of hash associative array

Steering script

The code is as follows Copy Code

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

Interactive usage Examples
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 not defined, return www.111cn.net URL by default
@param Choice Select name
@return The actual URL of the URL
function GetURL (choice) {
url = urlhash[choice];
if (typeof (Urlhash[choice]) = = "undefined")
url = "Www.111cn.net";
return URL;
}

Get all keys to 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>


For example:

The main point here is to illustrate the traversal of an associative array, first defining an array:

The code is as follows Copy Code

var arr = new Array ();

The data that creates an associative array casually is as follows:

arr["Name" = "Mary";

Arr["age"] = "3";

arr["sex"] = "man";

Use the For loop to iterate through the following:
for (var keyin arr)
{
The value for the above key variable is "name" or "age" or "sex".
Rather than the value of the array
The following value is taken to the corresponding values
var value = Arr[key];
}

The properties of an object in JS can be in brackets "[]" or "." To access, such as the above a["a"] and a.a are equivalent.

Example

The following code creates and initializes an associative array that contains three elements (note the format):

The code is as follows Copy Code
var myarray = {"A": "Athens", "B": "Belgrade", "C": "Cairo"};

In this array, you can use a string ("a", "B", or "C") to address the element, rather than using the number of the array element (0, 1, or 2) to address.
This allows you to create and use arrays in a more intuitive addressing scenario. You can also use the For...in statement code shown above to traverse the array.

For (key in myarray)
document.write ("Element value is" + Myarray[key] + "<BR>);

===========================
Example:

  code is as follows copy code

<script>
var myarray = {"A": "Athens", "B": "Belgrade", "C": "Cairo"};
for (key in myarray)
{
  document.write (key+ "=" +myarray[key] + "<BR>");
}

document.write ("a=" +myarray["a"]);

</script>

Related Article

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.