7
An array index is just a special type of object property name
A=new Array ()
[]
a[-3.15]= "I m-3.15"
"I m-3.15"
A
[]
A[-3.15]
"I m-3.15"
a['-3.15 ']
"I m-3.15"
A[3]=function () {Console.log (' This is a function! ')}
function () {Console.log (' This is a function! ')}
A[3]
function () {Console.log (' This is a function! ')}
A[3] ()
This is a function!
a[' 3 '] ()
7.3 Sparse arrays are arrays that contain discontinuous indexes starting at 0. If it is a sparse array, the Length property value is greater than the number of elements. You can create a sparse array with an array () constructor or a simple specified array whose index value is greater than the current array length
7.4 Array length
Setting the array length after the array is created will delete or add the corresponding element
a = [1,2,3,4,5];
a.length = 3; A for [a]
You can use Object.defineproperty () to make the length property of the array read-only, which will prevent changing the array length
A = [n/a];
Object.defineproperty (A, ' length ', {writable:false});
a.length=0; A will not change
7.6 Array Traversal
for ()
Data.foreach ()
Data.foreach (function (x) {
console.log (x)
});
7.7 Multi-dimensional arrays
JavaScript does not support true multidimensional arrays, and can be approximated by arrays of arrays. Simply use the [] operator two times.
a=[];a[1]=[];
7.8 Array Methods
Array.join () Converts all elements of an array into strings concatenated together, returning the resulting string
var a=[1,2,3];
A.join ()//' The "
A.join (")//' 123 '
A.join (')//' 1 2 3 '
A.join ('-')//' "
Array.reverse () returns an array of reverse order
Sort is returned in alphabetical order
Concat () connection itself and parameters if the parameter is an array will concatenate the array
Slice () returns the position of the defined array-1 represents the last element
Splice () inserting or deleting elements
The first parameter represents the beginning of the number of elements, and the second parameter represents the deletion of a few subsequent elements that are specified to be inserted into the array to return the deleted element element itself to change
var a = [1,2,3,4];
b = A.splice (2,1, ' A ', ' B ');
b = [3]
A=[1, 2, "a", "B", 4]
Push adds elements to the array at the end and returns the new length of the array
Pop deletes the last element of the element and returns the deleted value
Unshift inserts a new element at the beginning of the array and returns the new length
Shift deletes the first element of the array and returns the deleted value
ToString and join do not use parameters like
toLocaleString don't understand.
ForEach iterates through the array, calling the specified function for each element does not terminate convenience
Map calls each element to the specified function and returns an array if the sparse array is also the same way as a sparse array
Filter returns an array of elements that are called an array of one's own passed function is used to logically determine if the return value is TRUE or the value that can be converted to true is the element passed to the decision function is this own member
var a = [1,2,3,4,5,6];
A.filter (function (x) {return x < 3})//[+]
The filter skips the missing elements in the sparse array and returns always dense, which can be used to compress the sparse array a.filter (function (x) {return true})
Every and some return a Boolean value by applying the specified function to the element
The Every call decision function returns True when all elements are passed to the decision function, and it returns true
Some returns false when there is a function that returns True when it returns all functions
Reduce reduceright uses the specified function to combine arrays to generate a single value
var a= [1,2,3,4,5]
var sum = a.reduce (function (x, y) {return x+y},0)
If you do not set the second argument, then the first two arguments passed into the specified function will be the values of the first two elements of the array
Reduceright and reduce work the same way that the array is processed from the high to the low of the array index
IndexOf all the elements of a given value in an array returns the index of the first element found the second parameter is optional from the array where the index is not found return-1 indexOf from the beginning to the end lastIndexOf from the back to the front
Find the index of all matching elements
function FindAll (a,x) {
var results = [];
len = a.length,
pos = 0;
while (Pos < len) {
pos = a.indexof (X,pos);
if (pos = = =-1) break;
Results.push (POS);
pos = pos + 1;
}
return results;
}
7.1 Array Type
You can write this in es5.
Array.isarray ();
Instanceof Array is unreliable
var IsArray = Function.isarray | | Function (o) {
return typeof o = = = ' object ' &&
Object.prototype.toString.call (o) = = = ' [Object Array] ';
};
7.12 as an array of strings
var s = ' Test ';
S.charat (0);
S[1];
JavaScript Authoritative Guide Learning note the seventh chapter of an array