Three things you don't know about JavaScript Arrays

Source: Internet
Author: User
In programming languages, Array is a very common function. It is a special variable that can be used to store multiple values at the same time. However, in JavaScript, there are many other aspects worth exploring about the array function ....

In programming languages, Array is a very common function. It is a special variable that can be used to store multiple values at the same time. However, in JavaScript, there are many other aspects worth exploring about the Array Function.

In this article, we will discuss three less common functions of JavaScript arrays.

1. Add custom attributes to the array

When searching for definitions of JavaScript arrays on the internet, you will find that almost all users have the same definition of Arrays: an object.

In fact, everything we process with JavaScript can be considered as an object. There are two data types in JavaScript: basic data type, but basic types are basically included in the object type.

Arrays, functions, and dates are predefined objects in JavaScript. They all contain methods, attributes, and their respective standard syntaxes.

JavaScript arrays have the following three attributes:

The index of array 1 is also its attribute.

2 built-in attributes

3. You can add custom attributes by yourself.

The first two attributes are well-known. You may be using them every day, but I still want to say a few more here, and then I will talk about how to add custom attributes to arrays.

Using indexes as attributes

JavaScript Arrays can use square brackets, such as var ary = ["orange", "apple", "lychee"];.

The index of an array element is basically an attribute, and its attribute name is always a non-negative integer.

The index element pair of the array is similar to the key value pair of an object. Indexes are unique features of array objects. Different from other built-in attributes, they can be configured separately using square brackets, such as ary [3] = "peach ";.

Built-in attributes

The array has built-in attributes, such as array. length. This Length attribute contains an integer to indicate the length of the array.

Generally, built-in attributes can be found in pre-defined JavaScript objects such as arrays. Built-in attributes are combined with built-in methods. They can customize common objects to meet different requirements.

When accessing the built-in attributes, you can use two syntaxes: object. key or object ["key"]. That is to say, when obtaining the array length, you can write it as ary ["length"].

Create custom attributes for array objects

Now let's talk about how to add custom attributes to arrays. An array is a predefined object that stores different types of values in different indexes.

Generally, we do not need to add custom attributes to arrays. For this reason, when we first learned JavaScript, no one told us that we can add attributes to arrays. In fact, if you want to add a key-value pair to an array like a normal object, you can also use a normal object for the purpose. However, this does not mean that there are no special cases. In some cases, you can add one or more custom attributes to an array by using the fact that arrays are also an object.

For example, you can add a custom attribute that can recognize the element "kind" or "class" to the array. For details, see the following example:

 var ary = ["orange","apple","lychee"];ary.itemClass = "fruits";console.log(ary + " are " + ary.itemClass);

Please note that the custom attributes you add to the array are only a few, that is, they can be ...... Such as in.

2. loop through array elements

You may say, "I already know this ." You already know how to index array elements. However, if you say "loop in array elements", you may think it is a bit abstract, because what we actually loop is the index of the array.

Because array indexes are composed of non-negative integers, we usually start from 0 until the full length of the array to iterate the integer, then, the iterative value is used to obtain the array elements based on the specific index.

However, since ECMAScript6 emerged, we can loop through the array values instead of the index, and this operation can use ...... Of loop.

In the array, ...... The loop can loop array elements according to the index order. In other words, it can take charge of index iteration and obtain an existing array value based on the given index. If you just want to loop through all array elements and use them, this loop is very useful.

 var ary = ["orange","apple","lychee"];for (let item of ary){  console.log(item);}For comparison, with the regular for loop, we get the indices instead of the values as output. var ary = ["orange","apple","lychee"];for (var item = 0; item < ary.length; item++){  console.log(item);}
3. The number of elements is not the same as its length.

Generally, when we talk about the length of an array, we think that the length is either the number of array values or the length we manually set for the array. But in fact, the length of the array depends on its largest existing index.

Length is a very flexible attribute. Whether or not you have adjusted the length of the array, the length of the array will increase as long as you constantly add new values to the array.

 var ary = [];ary.length = 3;console.log(ary.length);ary[5] = "abcd";console.log(ary.length);

In the preceding example, you can see that only one value is specified for index 5 of the array, and the length is changed to 6. Now, if you think that if you add a value to index 5, the array will automatically create the index 0-4, then your speculation will be wrong. The array does not contain indexes 0-4. You can use in operator to view the information.

 var ary = [];ary.length = 3;console.log(ary.length);ary[5] = "abcd";console.log(ary.length);console.log(0 in ary);

The above ary array is called a sparse array. The index of this array will not be created continuously, and there is air between indexes. The opposite of the sparse array is a dense array (dense array ). The index of the dense array is continuously created, and the number of elements is equal to its length.

The Length attribute of the array can also be used to shorten the number and ensure that the maximum number of indexes in the array is always smaller than that of the array itself, because by default, the length value is always greater than the maximum number of indexes.

In the following example, you can see that I reduced the length of the ary array to generate the elements in index 5.

var ary = [];ary.length = 3;console.log(ary.length);ary[5] = "abcd";console.log(ary.length);ary.length = 2;console.log(ary.length);console.log(ary[5]);

The above is about the JavaScript array, the content of three things you don't know. For more information, please follow the PHP Chinese Network (www.php1.cn )!

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.