Grammar, chapter sixth, array

Source: Internet
Author: User
Tags array length pow

(This article is the Nanyi JS Standard Tutorial's study note, aims to summarize the knowledge points involved in the tutorial and some of the expansion of the individual, convenient as a "directory" or "outline" review and leak clearance, details see Nanyi Tutorial original)

Part Two syntax

Sixth Chapter Array ***********

I. Definition of an array
1. Concept: A set of numbers in order, each with a number (starting at 0)
The entire array is represented by []
2. You can define the time assignment, or you can assign a value later.
Arr[0]= ' a ';
3. Any data type can be put into an array, [1, ' 1 ', [1,2],{},function () {}]
4. The elements of the array are still arrays, forming a multidimensional array;

var a = [[1, 2], [3, 4]];
A[0][1]//2
A[1][1]//4

Ii. the nature of the array

1. In essence, an array belongs to a special object. The typeof operator returns an array of type object.

typeof [1, 2, 3]//"Object"

2. Array as the special body of the object now, its key name is the natural number in sequence (0,1,2,3 ...)
var arr = [' A ', ' B ', ' C '];

Object.keys (arr)
["0", "1", "2"]

The Object.keys method returns all the key names of the array. You can see that the key names of the arrays are integers 0, 1, 2.
JS internally treats an array as an object, except that the external representation is different.


3. Because the key names of the array members are fixed, the array does not have to specify a key name for each element, and each member of the object must specify the key name.

The 4.JavaScript language specifies that the key name of an object is a string, so the key name of the array is actually a string. A numeric value can be read because a non-string key name is converted to a string.

var arr = [' A ', ' B ', ' C '];

arr[' 0 '//' a ' standard read the key value of a key value pair in an object should be written like this
ARR[0]//' A ' numeric key name is automatically converted to a string


5.var arr = [1, 2, 3];
arr.0//SyntaxError

In the above code, the arr.0 is not valid because the individual values cannot be used as identifiers (identifier). This point will be the JS engine is considered a decimal point, only arr[0]

6. "The value of the shaping operation occurs before the type conversion, such as String (0x86), the 0x86 is first shaped to 134, and then the data type is" 134 "
var a=[];


a[1.00]=6;

A[1.000000000000000000]//
6

a["1.00000"]//
Undefined
a["1.0000"]=8; This is an array member that has a custom index (key name) of "1.0000" value of 8 in the array
A[1.0000]//6;
a["1.0000"]//8
A[0X1]
66 binary number will be first shaped to decimal 1 and then to string "1"

Third, Length property
1. Returns the number of members of the array.
2.JavaScript uses a 32-bit integer to hold the number of elements in the array. This means that the array member has a maximum of 4,294,967,295 (232-1), which means that the maximum value of the length property is 4294967295.
3. As long as it is an array, there must be a length attribute.
The property is a dynamic value, equal to the maximum integer in the key name plus 1. In addition, this indicates that the array is a dynamic data structure that can be added or subtracted from the array's members at any time.

var arr = [' A ', ' B '];
Arr.length//2

ARR[2] = ' C ';
Arr.length//3

ARR[9] = ' d ';
Arr.length//10

ARR[1000] = ' e ';
Arr.length//1001

The numeric keys of an array do not need to be contiguous.

The 4.length attribute is not only a characteristic indicator of an array, it is writable, and his modifications are reversed to arr.
5. Because the standard array members do not need to write their own key name, the default key name is 0,1,2,3 ..., and the array only use the default key name of the member (do not require the subscript continuous, arbitrary), can be recorded by the length property or operation, of course, you can add an array of key names is not natural number of properties, It will not affect length, nor will it be affected by the man-made change of length.

6. The key name of the array is added out of range values, the key name is automatically converted to a string, and the ARR array is no longer a standard array, but a hybrid.
var arr=[];
Arr[-1]= ' a ';
Arr[math.pow (2,32)]= ' B ';
Arr[1.2]= ' C ';
arr.length;//0;
ARR[-1]//"a"
ARR[4294967296]//"B"
The last two rows fetch a value because the [] operator automatically converts the value inside to a string when the key value is taken.

arr//["-1": "A", "2^32": "B", "1.2": "C"]

7.length assignment: Affects only the members of the array in which the subscript meets the requirements, the index is the member of the natural number, and the members of the other custom subscript are not involved in the discussion
7.1 Artificially set a value that is less than the current number of members.
This number is automatically reduced to the value set by length;
var arr=[' A ', ' B ', ' C ']
arr.length=2;
Arr//[' A ', ' B '];

7.2 Set length is greater than the current number of elements, the length of the indicator, the array members will increase to this value,
Read the values on these new locations to return undefined;

var arr=[];
arr.length=10;
To open 10 empty storage locations;

ARR[1]//undefined;
document.write (arr) can see the,,,,,,,,, printed on the screen

arr[0]=undefined;
Arr becomes index 0 is undefined, remaining nine empty storage locations;

"For". In traversal, you can traverse to the first undefined, and the back nine slots cannot be traversed "
"When checked with the in operator, null position returns FALSE, only 0 in arr returns true, so there is only one member in the array from the angle of the in operator"
"foreach traversal array also skips empty positions"
document.write (arr) is still printing out,,,,,,,,, instead of undefined,,,,,,,,,

7.3 Because the object read the non-existent property does not error but returns undefined, with the above situation to distinguish
var arr=["a"]
arr[50]//undefined;



7.4length disorderly assignment, as long as not 0~2^32-1 between the integer, waiting for the error!
Length set illegal value, JS error
Set negative values
[].length = 1
Rangeerror:invalid Array length

The number of array elements is greater than or equal to 2 of the 32-square
[].length = Math.pow (2, 32)
Rangeerror:invalid Array length

Set string
[].length = ' abc '
Rangeerror:invalid Array length


Four, array-like objects/pseudo-arrays
1. Some objects are called array-like objects that look like arrays, can use the Length property, but do not have an array, so the array method cannot be used;
var obj = {
0: ' A ',
1: ' B ',
2: ' C ',
Length:3
};

OBJ[0]//' a '
OBJ[2]//' C '
Obj.length//3
Obj.push (' d ')//TypeError:obj.push is not a function

2. An array-like object has only one feature, which is the length property, in other words, the object can be thought of as an array as long as it has the length property. However, the length property of an object is not a dynamic value and does not change as the member changes.
var obj = {
length:0
};
OBJ[3] = ' d ';
Obj.length//0

3. Typical array-like objects are arguments objects of functions, as well as most DOM element sets and strings.
Arguments Object
function args () {return arguments}
var arraylike = args (' A ', ' B ');

ARRAYLIKE[0]//' a '
Arraylike.length//2
Arraylike instanceof Array//False

DOM Element set
var ELTs = document.getelementsbytagname (' h3 ');
Elts.length//3
ELTs instanceof Array//False

String
' ABC ' [1]//' B '
' abc '. Length//3
' abc ' instanceof Array//False

4. The array's slice method converts an array-like object into a true array.
var arr = Array.prototype.slice.call (arraylike);
Unknown

5. Traversing an array-like object, you can take a for loop, or you can take an array of foreach methods.

For loop
function Logargs () {
for (var i = 0; i < arguments.length; i++) {
Console.log (i + '). ' + arguments[i]);
}
}

foreach method
function Logargs () {
Array.prototype.forEach.call (arguments, function (Elem, i) {
Console.log (i+ '. ' +elem);
});
}

Five, in operator;

1. Check whether a key name exists in the operator in, applies to the object, and also applies to the array.

var arr = [' A ', ' B ', ' C '];
2 in arr//True
' 2 ' in arr//True
4 in ARR//False

The above code indicates that the array has a key with a key name of 2. Because the key name is a string, the value 2 is automatically converted to a string

2. If a position of the array is empty, the in operator returns false.

var arr = [];
ARR[100] = ' a ';

+ arr//True
1 in ARR//False

In the above code, the array arr has only one member arr[100], and the key names in the other locations return false.

Vi. for: In loop and array traversal;
1. Iterating over arrays and objects, after all, arrays are just a special object.
var a = [1, 2, 3];

for (var i in a) {
Console.log (A[i]);
}
1
2
3

2.for...in not only iterates through all the numeric keys of an array, it also traverses non-numeric keys.
var a = [1, 2, 3];
A.foo = true;

for (var key in a) {
Console.log (key);
}
0
1
2
Foo

The above code, when traversing an array, also iterates over the non-integer key foo. Therefore, it is not recommended to use for...in to iterate through an array.
3. The traversal of an array can be considered using a for loop or while loop.

var a = [1, 2, 3];

For loop
for (var i = 0; i < a.length; i++) {
Console.log (A[i]);
}

While loop
var i = 0;
while (I < a.length) {
Console.log (A[i]);
i++;
}

var L = a.length;
while (l--) {
Console.log (A[l]);
}

The last way to do this is to traverse backward, i.e. from the last element to the first element.

The 4.forEach method object cannot be used, which targets the array, and for the members of the integer index, ignores the empty position.
The array's foreach method can also be used to iterate over an array, as described in the section of the array object in the standard library chapter.

var colors = [' red ', ' green ', ' Blue '];
Colors.foreach (function (color) {
Console.log (color);
});

Seven, the empty array
1. When a position of an array is an empty element, that is, there is no value between the two commas, we call the array empty (hole).

var a = [1,, 1];
A.length//3

2. The empty space is also counted in the length attribute
3. The last element is followed by a comma, and does not produce a vacancy, and there is no such comma as the result
var a=[1,2,3,]
A.length//3
4. The empty array can be read and returned to undefined. But you use the in operator to check his index and return false, meaning you can't find
var a = [,,,];
A[1]//undefined

5. Deleting an array member using the Delete command creates an empty space and does not affect the length property.

var a = [1, 2, 3];
Delete A[1];

A[1]//undefined
A.length//3

6. The above code deletes the second element of the array with the Delete command, which forms an empty space, but has no effect on the length property. In other words, the Length property does not filter the empty space. Therefore, use the Length property for array traversal and be very careful.
7. A position in the array is empty, and a position is undefined, which is not the same. If it is empty, the empty space is skipped by using the array's foreach method, the for...in structure, and the Object.keys method for traversal.
8. If a position is undefined, the traversal will not be skipped.

var a = [Undefined, undefined, undefined];

A.foreach (function (x, i) {
Console.log (i + '). ' + x ');
});
0. Undefined
1. Undefined
2. Undefined

for (var i in a) {
Console.log (i);
}
0
1
2

Object.keys (a)
[' 0 ', ' 1 ', ' 2 ']

9. The empty space is the array without this element, so it is not traversed, and undefined means that the array has this element, the value is undefined, so the traversal will not be skipped.


Grammar, chapter sixth, array

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.