JavaScript Concise tutorial (3) array

Source: Internet
Author: User

Tags: Modify medium border highlight cin pass Hello search substring

The array of JavaScript can contain any data type, and each element is accessed through an index.
To get the length of the array, directly access the Long property:

[JavaScript]Plain Text view copy code ?
001002 vararr = [1, 2, 3.14, ‘Hello‘, null, true];arr.length; // 6

Note that assigning a new value directly to the length of the array will result in an array size change:

[JavaScript]Plain Text view copy code ?
001002003004005006 vararr = [1, 2, 3];arr.length; // 3arr.length = 6;arr; // arr变为[1, 2, 3, undefined, undefined, undefined]arr.length = 2;arr; // arr变为[1, 2]

An array can be indexed to modify the corresponding element to a new value, so assigning a value to an array's index modifies the array directly:

[JavaScript]Plain Text view copy code ?
001002003 vararr = [‘A‘, ‘B‘, ‘C‘];arr[1] = 99;arr; // arr现在变为[‘A‘, 99, ‘C‘]

Note that if an index is assigned a value, the index is out of range, which can also cause an array size change:

[JavaScript]Plain Text view copy code ?
001002003 vararr = [1, 2, 3];arr[5] = ‘x‘;arr; // arr变为[1, 2, 3, undefined, undefined, ‘x‘]

Most other programming languages do not allow direct changes to the size of the array, and an error occurs when accessing the index out of bounds. However, the array of JavaScript does not have any errors. When writing code, it is not recommended to modify the size of the array directly, and to ensure that the index does not cross over when accessing the index.

IndexOf
Like a string, an array can also search for the location of a specified element by indexof ():

[JavaScript]Plain Text view copy code ?
001002003004005 var arr = [Ten, ' xyz ' arr.indexof; //element 10 has an index of 0 arr.indexof; //element 20 has an index of 1 arr.indexof (in); //element 30 not found, returns-1 arr.indexof ( ' " ); //element ' 30 ' has an index of 2

Note that the number 30 and the string ' 30 ' are different elements.

Slice
Slice () is the substring () version of the corresponding string, which intercepts some elements of the array and returns a new array:

[JavaScript]Plain Text view copy code ?
001002003 var arr = [ ' A ' ' B ' ' C ' ' D ' ' E ' ' F ' ' G ' arr.slice (0, 3); //starts at index 0 and ends at index 3, but does not include index 3: [' A ', ' B ', ' C '] arr.slice (3); //starting from index 3 to end: [' D ', ' E ', ' F ', ' G ']

Note that the start and end parameters of slice () include the starting index, excluding the ending index.

If you do not pass any arguments to slice (), it will intercept all elements from beginning to end. With this, we can easily copy an array:

[JavaScript]Plain Text view copy code ?
001002003004 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 Poppush () add several elements to the end of the array, and pop () removes the last element of the array:

[JavaScript]Plain Text view copy code ?
001002003004005006007008009 var arr = [1, 2];arr.push(‘A‘, ‘B‘); // 返回Array新的长度: 4arr; // [1, 2, ‘A‘, ‘B‘]arr.pop(); // pop()返回‘B‘arr; // [1, 2, ‘A‘]arr.pop(); arr.pop(); arr.pop(); // 连续pop 3次arr; // []arr.pop(); // 空数组继续pop不会报错,而是返回undefinedarr; // []



Unshift and Shift If you want to add several elements to the head of an array, using the Unshift () method, the Shift () method deletes the first element of the array:

[JavaScript]Plain Text view copy code ?
001002003004005006007008009 var arr = [1, 2];arr.unshift(‘A‘, ‘B‘); // 返回Array新的长度: 4arr; // [‘A‘, ‘B‘, 1, 2]arr.shift(); // ‘A‘arr; // [‘B‘, 1, 2]arr.shift(); arr.shift(); arr.shift(); // 连续shift 3次arr; // []arr.shift(); // 空数组继续shift不会报错,而是返回undefinedarr; // []



Sort sort() sorts the current array, which directly modifies the position of the elements of the current array and, when called directly, in the default order:

[JavaScript]Plain Text view copy code ?
001002003 vararr = [‘B‘, ‘C‘, ‘A‘];arr.sort();arr; // [‘A‘, ‘B‘, ‘C‘]


Can we sort them in the order we specify? Absolutely, we'll talk about it in the function that follows.

Reverse reverse () gives all the elements of the array, namely the reversal:

[JavaScript]Plain Text view copy code ?
001002003 vararr = [‘one‘, ‘two‘, ‘three‘];arr.reverse(); arr; // [‘three‘, ‘two‘, ‘one‘]



Splice

The Splice () method modifies The "Universal method" of an Array, which deletes several elements from the specified index, and then adds several elements from that location: [JavaScript]Plain Text view copy code ?
001002003004005006007008009 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‘]


ConcatThe concat () method joins the current array with another array and returns a new array:[JavaScript]Plain Text view copy code ?
001002003004 var arr = [ ' A ' ' B ' Code class= "JavaScript plain", ' C ' ; var added = Arr.concat ([1, 2, 3]); added; //[' A ', ' B ', ' C ', 1, 2, 3] arr; //[' A ', ' B ', ' C ']
Please note ,the concat () method does not modify the current array, but instead returns a new array.
in fact, the concat () method can receive any element and array, and automatically takes the array apart and adds it all to the new array: [JavaScript]Plain Text view copy code ?
001002 vararr = [‘A‘, ‘B‘, ‘C‘];arr.concat(1, 2, [3, 4]); // [‘A‘, ‘B‘, ‘C‘, 1, 2, 3, 4]


JoinThe Join () method is a useful way to concatenate each element of the current array with a specified string and then return the concatenated string:[JavaScript]Plain Text view copy code ?
001002 vararr = [‘A‘, ‘B‘, ‘C‘, 1, 2, 3];arr.join(‘-‘); // ‘A-B-C-1-2-3‘
If the element of the Array is not a string, it is automatically converted to a string and then concatenated. http://www.ququer.org/Sodu novels

JavaScript Concise tutorial (3) array

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.

Tags Index: