JavaScript Array (v)

Source: Internet
Author: User

Create arrays, array operations

An array is an ordered set of points. Each value is called an element, and each element has a comb position number in the array, which is the index. The array in JS is a weak type , and the array can contain different types of elements. An array element can even be an object or other array.

1. Create an array of literal quantities

var arr=[1,true,null,undefined,{x:1},[1,2,3]]

You can create a literal array with a comma interval, but the readability is poor and not recommended.

The 2 comma (,) in the brackets will be interpreted as 2 undefined, and the last comma to the right will be ignored .

2. Array size

1~4,294,967,295 (2^23-1)

3. Create an array with the new array

The new Array () parameter can be null or, one, or multiple parameters.

var arr=New Array (); // equivalent to arr=[]; var arrwithlength=New Array (100); // undefined*100 var arrlikesliteral=New Array (true,false,null, OK, "HI"); // equivalent to [true,false,null,1,2, "HI"]

New array can also be omitted.

4. Reading and writing of array elements

The length does not change after you delete an array element with delete

Add array elements

4 ways to add array elements

var arr=[];arr[0]=1; arr[1]=2; Arr.push (3); arr; // [A] Arr[arr.length]=4;      //  arr; // [1, 2, 3, 4] Arr.unshift (0);        // head increased arr; // [0, 1, 2, 3, 4]

Delete an array element

Delete Deletes an array element by actually turning the array element into undefined.

4 ways to delete an array element are as follows:

Arr//[0, 1, 2, 3, 4]DeleteArr[2]; arr;//[0, 1, undefinedx1, 3, 4]arr.length;//52incharr//falseArr.length-=1arr;//[0, 1, undefinedx1, 3],4 is removedArr.pop ();//3 returned by poparr;//[0, 1, undefinedx1],3 is removedArr.shift ();//0 returned by shiftarr;//[1, undefinedx1]
Two or two-d arrays, sparse arrays

Sparse arrays: Sparse arrays do not contain contiguous indexes starting at 0. The value of the normal length property is greater than the actual element number.

Using the new Array (1) just creates an array of length 1, and the array subscript 0"key 0" does not exist.

The length of the array can be changed dynamically, and the array length becomes 100.

Although the array length is changed to 100 through Arr1.length, JavaScript does not automatically create other keys from 0 to 98.

An array created with a comma (,) blank is also a sparse array.

Use the in operator when traversing sparse arrays, or determine if it is undefined.

Three, array method

There are many ways to prototype an array.

1, Array.prototype.join

The join () method converts an array to a string . commonly used .

Application scenarios such as spelling some URL parameters, print the array.

Join () does not pass parameters by default, separated by commas.

Join () pass in parameters, with parameters as delimiters for each element of the array.

Write a function to repeat the creation of a string n times.

Undefined is ignored when an empty array is dropped to join.

2, Array.prototype.reverse

The reverse () method reverse the array. commonly used . The original array is modified.

3, Array.prototype.sort

Sort. The original array is modified. in place

Sort is sorted alphabetically by default.

Custom sorting

From small to large:

If you want to use b-a from the boulevard.

sorts the objects. The sort results can be viewed with foreach.

4, Array.prototype.concat

Array merging . The original array has not been modified .

Array elements are leveled as arguments. The array will only be flattened once.

If the argument is an array, and the element of the array is an array, the array elements inside will be flattened as a result.

var arr=[1,2,3];arr.concat (4,5); // [1,2,3,4,5] arr; // The original array was not modified Arr.concat ([10,11],13); // [1,2,3,10,11,13]arr.concat ([1,[2,3]]); // [1,2,3,1,[2,3]]
5, Array.prototype.slice Slice

Returns a partial array. The array fragment is returned. The original array has not been modified

left closed right open interval.

    • The second argument can be omitted, representing the last element at the beginning of the first parameter index
    • A negative index indicates a backward forward
6, Array.prototype.spliceAdhesive Bonding

Splice () A number of concatenation of the array, not only can delete some elements of the arrays, you can also join some new elements.

The original array is modified

Splice receives 1 Parameters:

Splice (2) starts from the 3rd element to the last as the return value and is removed.

Splice receives 2 parameters : The first parameter indicates which element to start with, and the second parameter represents the number of elements you want to delete .

Splice Receive multiple parameters:

Add a new element while deleting the element.

Starting with the 2nd element, delete the 1 elements and add "a" and "B" in the deleted position.

7, Array.prototype.forEach(ES5)

8, Array.prototype.map(ES5)

9, Array.prototype.filter(ES5)

10, Array.prototype.every(ES5)

11, Array.prototype.some(ES5)

12, Array.prototype.reduce/reduceright(ES5)

13, Array.prototype.indexof/lastindexof(ES5)

14, Array.isarray(ES5)

The author starof, because the knowledge itself in the change, the author is also constantly learning and growth, the content of the article is not updated regularly, in order to avoid misleading readers, convenient tracing, please reprint annotated source: http://www.cnblogs.com/starof/p/4904929. HTML has a problem welcome to discuss with me, common progress.

JavaScript Array (v)

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.