Arrays and array object

There are many beginners may feel the two are the same, I used to think so. But they are not the same, although they are arrays. Just a collection worth an array, and the array object is a collection of objects; wider array of objects.
An array is a collection worth, each has a value of an index number, the index starts from 0 and each index has a corresponding value, add more values as needed. There are an array of one-dimensional array, such as a two-dimensional array.
We know that the variables used to store data, a variable can store only one content. If you need to store more data a variable that can not be met. For example: There is a class of student achievement needs to be stored, if a variable to a storage becomes more troublesome. This time it will use the array, and the results are a class of students into an array inside.
As shown below:
Here Insert Picture Description
To use you need to create an array before the array, then how to create an array of it?
The first assignment to a variable array itself, then give this array assignment. Create an array at the same time can also specify the length of the array, you can specify any length. While at the same time also create an array of specified length, but are actually array becomes long. That even if you specify the length of the elements may still be stored outside of the specified length.
Just create a good array is an empty array, no value if the output is displayed as undefined. So give the array assignment. Assignment There are two ways: one is assigned while creating arrays; there is a direct input an array (also called literal array). FIG follows:
Here Insert Picture Description
When using the array index will need to obtain a value of the array element, it is necessary to provide a reference index and an array variable. Of particular note is the index starts at 0.
Array object is a collection of objects, which may be a collection of different types. Each member has an array of objects index, used to represent his position in the array, starting with zero. Although the definition of an array, the array is empty, but it specifies the definition of n empty array elements, and defining an array initialization data directly; example: var array name = [<element a>, <element two> <three element> ...]; in use when the array needs to use a standard element, the target between braces, scratch.
An array of objects have properties: length; Usage: <array object>. length; returns the length of the array, i.e. the number of array elements, it is equal to the number in the index of the last element plus one. The property then constructor Array () will create an array initialization; when adding new elements to an array (if necessary) will update the value of length. Setting the length property that can change the size of the array. If the value is smaller than its current value, the array will be truncated, it is not element within the length value will be lost. If the value is larger than its current value, the array is increased, the new element is added to the end of the array, which is undefined.
An array of objects There are many ways, not all listed here, simply say a few.
Array chaining method: contact (); contact () method is used to connect two or more arrays, returns a new array, the array does not change the original. If the parameter Contact () operation is an array, the array is then added to the elements, instead of an array.
Here Insert Picture Description
The method of connecting the specified delimiter array elements: join (); join () method for all elements in the array into a string. Elements are separated by a specified delimiter. He has a parameter: separator; character or string for the partition array elements in the returned string, which is optional. If this parameter is omitted, it will be comma as a delimiter. Its return value is a string, by converting each element of the array into a string, and then connecting these strings, separator strings specified inserted between the two elements. Returns the resulting string. This method does not affect the original contents of the array.
Here Insert Picture Description
The array is ordered method: sort (); sort () method is an array of elements arranged according to a certain sort order. Sorting is sorted in the original array, without making a copy. It also has a parameter: orderfunc (); optional. If the calling method sort () parameter is not used, in alphabetical order (more accurate to say, in the order the Unicode code) of the array elements are sorted. To achieve this, the first element of the array is converted to a string (if necessary), for comparison. If you want to be sorted by another sort, it is necessary to provide a function to compare the two function values, and then returns a number for explaining the relative order of these two. Even a comparison function should have parameters a and B, the return value is as follows:
If the return value <= - 1, then A occurs before B in the sorted sequence.
If the return value> -1 && <1, it indicates that A and B have the same sort order.
If the return value> = 1, A appears after B in the ordered sequence.
Here Insert Picture Description
Sorted in ascending order. After the output shown below:
Here Insert Picture Description
Note that undefined array elements are arranged at the end of the array. Even if you provide a custom sort function, too, because the undefined value will not be passed to orderfunc you provide.
An array of relatively simple point, an array of objects have properties and methods, is more complicated.
If the above there is something wrong, please contact me, I will promptly change.

Guess you like

Origin blog.csdn.net/weixin_44560796/article/details/91353687