JavaScript Array
can contain arbitrary data types, and each element is accessed by index.
To get Array
the length, access the length
property directly:
var arr = [1, 2, 3.14, 'Hello', null, true];
arr.length; // 6
Note that assigning a new value directly to Array
, results in a change in size:length
Array
var arr = [1, 2, 3];
arr.length; // 3
arr.length = 6;
arr; // arr变为[1, 2, 3, undefined, undefined, undefined]
arr.length = 2;
arr; // arr变为[1, 2]
Array
The corresponding element can be modified to a new value through the index, so Array
assigning an index to the index will directly modify this Array
:
var arr = ['A', 'B', 'C'];
arr[1] = 99;
arr; // arr现在变为['A', 99, 'C']
Note that if the index exceeds the range when assigning by index, the Array
size will also change:
var arr = [1, 2, 3];
arr[5] = 'x';
arr; // arr变为[1, 2, 3, undefined, undefined, 'x']
Most other programming languages do not allow changing the size of an array directly, and out-of-bounds access to an index will throw an error. However, JavaScript Array
does not have any errors. When writing code, it is not recommended to modify Array
the size directly, and when accessing the index, make sure that the index does not go out of bounds.
indexOf
Similar to String, the position of a specified element Array
can also be searched by:indexOf()
var arr = [10, 20, '30', 'xyz'];
arr.indexOf(10); // 元素10的索引为0
arr.indexOf(20); // 元素20的索引为1
arr.indexOf(30); // 元素30没有找到,返回-1
arr.indexOf('30'); // 元素'30'的索引为2
Note that numbers 30
and strings '30'
are different elements.
slice
slice()
It is the version corresponding to String substring()
, which intercepts Array
some elements and returns a new one Array
:
var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
arr.slice(0, 3); // 从索引0开始,到索引3结束,但不包括索引3: ['A', 'B', 'C']
arr.slice(3); // 从索引3开始到结束: ['D', 'E', 'F', 'G']
Note that slice()
the start and end parameters include the start index, but not the end index.
If no slice()
arguments are passed, it will truncate all elements from beginning to end. Taking advantage of this, we can easily replicate one Array
:
var arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
var aCopy = arr.slice();
aCopy; // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
aCopy === arr; // false
push and pop
push()
Array
Add several elements to the end of , pop()
delete Array
the last element of :
var arr = [1, 2];
arr.push('A', 'B'); // 返回Array新的长度: 4
arr; // [1, 2, 'A', 'B']
arr.pop(); // pop()返回'B'
arr; // [1, 2, 'A']
arr.pop(); arr.pop(); arr.pop(); // 连续pop 3次
arr; // []
arr.pop(); // 空数组继续pop不会报错,而是返回undefined
arr; // []
unshift和shift
If you want to Array
add several elements to the head, use the unshift()
method, shift()
the method will Array
delete the first element:
var arr = [1, 2];
arr.unshift('A', 'B'); // 返回Array新的长度: 4
arr; // ['A', 'B', 1, 2]
arr.shift(); // 'A'
arr; // ['B', 1, 2]
arr.shift(); arr.shift(); arr.shift(); // 连续shift 3次
arr; // []
arr.shift(); // 空数组继续shift不会报错,而是返回undefined
arr; // []
sort
sort()
The current can Array
be sorted, it will directly modify the current Array
element position, and when it is called directly, it will be sorted according to the default order:
var arr = ['B', 'C', 'A'];
arr.sort();
arr; // ['A', 'B', 'C']
Can we sort them in the order we specify? Absolutely, we'll talk about it later in the function.
reverse
reverse()
Give the whole Array
element one by one, that is, invert:
var arr = ['one', 'two', 'three'];
arr.reverse();
arr; // ['three', 'two', 'one']
splice
splice()
The method is a modified Array
"catch-all method" that removes several elements starting at a specified index, and then adds several elements from that position:
var arr = ['Microsoft', 'Apple', 'Yahoo', 'AOL', 'Excite', 'Oracle'];
// 从索引2开始删除3个元素,然后再添加两个元素:
arr.splice(2, 3, 'Google', 'Facebook'); // 返回删除的元素 ['Yahoo', 'AOL', 'Excite']
arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
// 只删除,不添加:
arr.splice(2, 2); // ['Google', 'Facebook']
arr; // ['Microsoft', 'Apple', 'Oracle']
// 只添加,不删除:
arr.splice(2, 0, 'Google', 'Facebook'); // 返回[],因为没有删除任何元素
arr; // ['Microsoft', 'Apple', 'Google', 'Facebook', 'Oracle']
concat
concat()
The method joins the current one Array
with another Array
and returns a new one Array
:
var arr = ['A', 'B', 'C'];
var added = arr.concat([1, 2, 3]);
added; // ['A', 'B', 'C', 1, 2, 3]
arr; // ['A', 'B', 'C']
Note that the concat()
method does not modify the current one Array
, but returns a new one Array
.
In fact, concat()
methods can take any number of elements and Array
automatically Array
unpack them and add them all to a new one Array
:
var arr = ['A', 'B', 'C'];
arr.concat(1, 2, [3, 4]); // ['A', 'B', 'C', 1, 2, 3, 4]
join
join()
The method is a very practical method, it concatenates Array
each current element with the specified string, and then returns the concatenated string:
var arr = ['A', 'B', 'C', 1, 2, 3];
arr.join('-'); // 'A-B-C-1-2-3'
If Array
the element is not a string, it will be automatically converted to a string before concatenating.
Multidimensional Arrays
Multidimensional arrays can be formed if an element of the array is another one Array
, for example:
var arr = [[1, 2, 3], [400, 500, 600], '-'];
The above Array
contains 3 elements, of which the first two are themselves Array
.