JS Operation Array

Source: Internet
Author: User

JavaScript Array can contain arbitrary data types, and each element is accessed through an index.

To get Array the length, directly access the length properties:

var arr = [1, 2, 3.14, ‘Hello‘, null, true];arr.length; // 6

Please note that Array length assigning a new value directly to the given will result in a Array change in size:

var arr = [1, 2, 3];arr.length; // 3arr.length = 6;arr; // arr变为[1, 2, 3, undefined, undefined, undefined]arr.length = 2;arr; // arr变为[1, 2]

ArrayThe corresponding element can be modified by the index to a new value, so Array assigning a value to the index will modify this directly Array :

var arr = [‘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 Array a change in size:

var arr = [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, JavaScript Array does not have any errors. When writing code, it is not recommended to directly modify Array the size of the index to ensure that the index does not cross.

IndexOf

Similar to string, Array you can also indexOf() search for the position of a specified element by:

var arr = [10, 20, ‘30‘, ‘xyz‘];arr.indexOf(10); // 元素10的索引为0arr.indexOf(20); // 元素20的索引为1arr.indexOf(30); // 元素30没有找到,返回-1arr.indexOf(‘30‘); // 元素‘30‘的索引为2

Note that the numbers 30 and strings ‘30‘ are different elements.

Slice

slice()Is the version of the corresponding string substring() , which intercepts Array the part of the element and then 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‘]

Notice that slice() the start and end parameters include the starting index, not the ending index.

If you don't slice() pass any arguments, it intercepts all the elements from the beginning to the end. With this, we can easily copy 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()Arrayadd a number of elements to the end, pop() then Array delete the last element:

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 Array add several elements to the head, using the unshift() method, the shift() Array first element of the method is deleted:

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()You can sort the current Array , and it will directly modify the current Array element position and, when called directly, in 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 in the function that follows.

Reverse

reverse()Give the whole Array element away, that is, reverse:

var arr = [‘one‘, ‘two‘, ‘three‘];arr.reverse(); arr; // [‘three‘, ‘two‘, ‘one‘]
Splice

splice()Method is a modified Array "universal method" that removes several elements from the specified index, and then adds several elements from that location:

var arr = [' Microsoft ',' Apple ',' Yahoo ',' AOL ',' Excite ',' Oracle ']; //Delete 3 elements starting at index 2 and then add two elements: Arr.splice (2, 3, ' Google ', ' Facebook '); //return deleted elements [' Yahoo ', ' AOL ', ' Excite ']arr; //[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle '//delete only, do not add: Arr.splice (2, 2); //[' Google ', ' Facebook ']arr; //[' Microsoft ', ' Apple ', ' Oracle ']//Only add, do not delete: Arr.splice (2, 0, ' Google ', ' Facebook '); //return [], because no element was removed from arr; //[' Microsoft ', ' Apple ', ' Google ', ' Facebook ', ' Oracle ']         
Concat

concat()Method connects the current Array and the other 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 Array , but instead returns a new one Array .

In fact, the concat() method can receive any element and Array automatically Array disassemble it and add it all to the new one Array :

var arr = [‘A‘, ‘B‘, ‘C‘];arr.concat(1, 2, [3, 4]); // [‘A‘, ‘B‘, ‘C‘, 1, 2, 3, 4]
Join

join()method is a very useful method that Array connects each current element with a 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 is automatically converted to a string and then concatenated.

Multidimensional arrays

If an element of an array is another Array , a multidimensional array can be formed, for example:

var arr = [[1, 2, 3], [400, 500, 600], ‘-‘];

ArrayThe above consists of 3 elements, of which the first two elements are themselves Array .

Exercise: How to fetch 500 this value by index:

Summary

ArrayProvides a function to store a set of elements sequentially, and can be read and written by index.

Practice: At the freshman reception, you've got a list of new classmates, please sort it out: 欢迎XXX,XXX,XXX和XXX同学!

JS Operation 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.