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 = 0; i < a.length; i ) {
alert(a[i]) ;
}
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 as above This array (dictionary)
for (var i in computer_price) {
alert(i ": " computer_price[i]);
}
The i here is each key value of the dictionary. The output result is:
Acer: 500
Dell: 600
In addition, computer_price is a dictionary object, and each of its key values is an attribute. In other words, Acer is an attribute of computer_price. We can use it like this:
computer_price.Acer
Let’s look at the simplified declaration of dictionaries and arrays.
var array = [1, 2, 3]; // Array
var array2 = { "Acer": 500, "Dell": 600 }; // Dictionary
alert(array2.Acer); // 500
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.
Let’s take a look at how to traverse the properties of an object. We can use for in to iterate over the properties of an object.
function Computer(brand, price) {
this .brand = brand;
this.price = price;
}
var mycomputer = new Computer("Acer", 500);
for (var prop in mycomputer) {
alert( "computer[" prop "]=" mycomputer[prop]);
}
In the above code, Computer has two attributes, brand and price. So the output result is:
computer[brand]=Acer
computer[price]=500
The above usage can be used to see what attributes an object has. When you already know that the Computer object has a brand attribute, you can use
mycomputer.brand
or mycomputer[brand]
to get the attribute value.
Summary: Arrays in Javascript can also be used to make dictionaries. The key values of a dictionary are also properties of the dictionary object. When traversing the properties of an object, you can use for in.
Array traversal and properties
Although arrays are objects in JavaScript, there is no good reason to use a for in loop to traverse the array.
Conversely, there are some good reasons not to use for in to iterate over arrays.
Note: Arrays in JavaScript are not associative arrays.
There are only objects in JavaScript to manage key-value correspondence. But associative arrays maintain order, while objects do not.
Since the for in loop enumerates all properties on the prototype chain, the only way to filter these properties is to use the hasOwnProperty function,
so it will be many times slower than an ordinary for loop.
Traversal
In order to achieve the best performance of traversing the array, it is recommended to use the classic for loop.
var list = [1, 2, 3, 4, 5, ... 100000000];
for(var i = 0, l = list.length; i < l; i ) {
console.log(list[i]);
}
The above code has a processing, which is to cache the length of the array through l = list.length.
Although length is a property of the array, there is a performance overhead in accessing it in each loop.
Maybe the latest JavaScript engines have been optimized in this regard, but we cannot guarantee whether our code will run on these latest engines.
In fact, the way without caching the array length is much slower than the cached version.
length attribute The getter method of the
length attribute will simply return the length of the array, while the setter method will truncate the array.
var foo = [1, 2, 3, 4, 5, 6];
foo.length = 3;
foo; // [1, 2, 3]
foo .length = 6;
foo; // [1, 2, 3]
Translator’s Note:
View the value of foo in Firebug at this time: [1, 2 , 3, undefined, undefined, undefined]
But this result is not accurate. If you view the result of foo in Chrome's console, you will find that it is like this: [1, 2, 3]
Because in In JavaScript, undefined is a variable. Note that a variable is not a keyword, so the meanings of the above two results are completely different.
// Translator's Note: In order to verify, let's execute the following code to see whether the serial number 5 exists in foo.
5 in foo; // Returns false whether in Firebug or Chrome
foo[5] = undefined;
5 in foo; // Returns true whether in Firebug or Chrome
is set to length A smaller value will truncate the array, but increasing the length property has no effect on the array.
Conclusion
For better performance, it is recommended to use a normal for loop and cache the length property of the array.
Using for in to iterate over an array is considered bad coding practice and tends to generate errors and cause performance issues.