Javascript's array Array is both an array and a dictionary.
Let’s give an example to see how arrays are used.
The code copy is as follows:
var a = new Array();
a[0] = "Acer";
a[1] = "Dell";
for (var i in a) {
alert(i);
}
The above code creates an array, each element is a string object.
Then iterate over the array. Note that the results of i are 0 and 1, and the results of a[i] are the string.
This is very similar to the properties of the traversing object mentioned in the previous article.
Let’s take a look at the usage of the dictionary.
The code copy is as follows:
var computer_price = new Array();
computer_price["Acer"] = 500;
computer_price["Dell"] = 600;
alert(computer_price["Acer"]);
We can even traverse this array (dictionary) as above
The code copy is as follows:
for (var i in computer_price) {
alert(i + ": " + computer_price[i]);
}
Here i is each key value of the dictionary. The output result is:
The code copy is as follows:
Acer: 500
Dell: 600
Let’s take a look at the interesting things about Javascript, or the example above.
We can think of computer_price as a dictionary object, and each key value of it is a property.
In other words, Acer is an attribute of computer_price. We can use it like this: computer_price.Acer
Let’s take a look at the simplified declaration methods of dictionaries and arrays.
The code copy is as follows:
var array = [1, 2, 3]; // Array
var array2 = { "Acer": 500, "Dell": 600 }; // Dictionary
alert(array2.Acer); // 50
This way the statement of the dictionary is the same as before. In our example, Acer is a key value, and it is also an attribute of a dictionary object.
The above is the entire content of this article. I hope you like it. We will continue to update it in the future.