This article mainly briefly introduces Javascript arrays and dictionaries. Friends in need can refer to it.
Javascript’s array Array is both an array and a dictionary.
Let’s take an example to see how to use arrays.
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 traverse the array. Note that the result of i is 0 and 1, and the result of a[i] is a string.
This is very similar to the properties of traversing objects mentioned in the previous article.
Let’s take a look at how to use the dictionary.
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
for (var i in computer_price) { alert(i + ": " + computer_price[i]); }
where i is each key value of the dictionary. The output result is:
Acer: 500 Dell: 600
Next, let’s take a look at the interesting part of Javascript, still the above example.
We can think of computer_price as a dictionary object, and each key value is an attribute.
That is to say, 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 method of dictionaries and arrays.
var array = [1, 2, 3]; // 数组 var array2 = { "Acer": 500, "Dell": 600 }; // 字典 alert(array2.Acer); // 50
This declaration of the dictionary is the same as the previous one. In our example, Acer is a key value and can also be used as an attribute of the dictionary object.
The above is the entire content of this article. I hope you all like it. For more related tutorials, please visit JavaScript Video Tutorial!