A beginner's array of JavaScript [novice must see]_javascript skills

Source: Internet
Author: User

An array of definitions.

There are two ways to define an array:

1, var arr1 = []; Define an empty array

2, var arr2 = [1,2,3, "str1", "str2"]; Defines an array that has 5 elements.

3, var arr3 = new Array (3); Define an empty array

4, var arr4 = new Array (1,2,3, "str1", "str2"); Defines an array with a specified length of 5.

Read and write the elements of the array.

ARR[0]; Reading the first array element

Arr[0] = "STR1"; Changes the value of the first element of the array.

Three, sparse arrays.

A sparse array represents an array of discontinuous indexes starting from 0. Usually the length of an array represents the number of elements in an element, and if the array is sparse, the length property value is greater than the number of elements.

The in operator is used to detect if an element exists in a location, and note that undefined is also present.

such as: var a1 = [,,];

var a2 = new Array (3);

0 in A1; True, because a[0] has undefined elements

0 in A2; FALSE,A2 no elements at index 0

Four, the length of the array

The length property is used to flag the lengths of the array

such as: var arr = [1,2,3,4,5];

Arr.length; 5 arr Array has 5 elements

Addition and deletion of array elements

Push://Add an element to the end of the array

var arr = [1,2,3];

Arr.push (4,5); Arr into [1,2,3,4,5]

Delete://delete elements from a location in an array

var arr = [1,2,3]

Delete Arr[1]//arr changed to [1,,3]

1 in Arr//false

Six, the array of traversal

The traversal of an array is typically implemented using a For statement

var arr = [1,2,3,4,5];

for (var i = 0.i<arr.length;i++) {

if (!a[i]) continue; Skipping null,undefined and nonexistent elements

}

Seven, multidimensional array

A multidimensional array is an array of elements or arrays.

such as: var arr = [[1,2,3],[,4,5,6]];

ARR[1][1]; 5

Eight, array method

1. Join () is used to convert all elements in an array into strings and join together, and you can customize the connection characters

var arr = [1,2,3];

Arr.join (); => "1,2,3"

Arr.join ("= ="); => "1==2==3";

2, reverse () to reverse the order of the array elements

var arr = [1,2,3];

Arr.reverse (); Arr array into [3,2,1]

3, sort (); Used to sort elements within an array. You can pass in a function as a sort, or, if it is null, sorted alphabetically. undifined elements in the final line

var arr = [1,2,3];

A.sort (function (a,b) {

return a-b; Sort standard negative 0 positive number, compare results first return the smaller one

}); The value of the ARR array is [1,2,3] if the second condition changes to b-a the result is [3,2,1]

4, concat ()//To assemble a new array, return a new array

var arr = [1,2,3]

Arrnew = Arr.concat (4,5)//arrnew array is [1,2,3,4,5]

Arrnew1 = Arr.concat ([4,5],[6,7]); Arrnew1 array is [1,2,3,4,5,6,7]

5. Slice ()//An array of elements used to return the specified interval of an array, or an array from this parameter to the end if you enter a parameter. Two parameters are the first argument is the starting position and the second argument is the number.

var arr = [1,2,3,4,5];

var arr1 = Arr.slice (2); [3,4,5]

var arr2 = Arr.slice (1,3); [2,3]

6, splice () delete or add elements. Changes the original array itself, equivalent to the reference in C # (ref), the original array is an array of deleted elements, and the return value is an array of the remaining elements.

var arr = [1,2,3,4,5];

var arr1 = Arr.splice (1,3); ARR is [2,3,4], the returned array is arr1 [1,5]

var arr2 = [1,2,3,4,5];

var arr3 = Arr2.splice (2,0, ' A ', ' B '); Delete from the 2nd bit, delete two elements, then insert ' a ', ' B ' from that position, arr2 as [], because no elements are deleted, arr3[1,2, ' A ', ' B ', 3,4,5]

7. Push () and pop () add or remove an element at the end of the array, and return it as the last added element when it is added, when it is deleted. Returns the element whose value is deleted.

The push () function adds an element to the tail of the array.

The pop () function deletes the last element of the array.

var arr = [1,2,3]

Arr.push (4); Arr for [1,2,3,4]

var arr1 = [1,2,3]

Arr.pop (); ARR1 for [1,2]

8, Unshift () and Shift ()

Shift (), unshift () and push (), pop () is simply the operation of the array head rather than the tail.

Shift () removes an element from the array header, and the returned value is the deleted element.

Unshift () Adds an element to the array header, returning the group as the last added element.

var arr = [1,2,3];

var a = Arr.shift (); Arr changed to [2,3] a to 1

var arr1 = [1,2,3];

var B = Arr1.unshift ([4,5]); ARR1 changed to [4,51,2,3],b 4 to return the last addition, add 5 before adding 4

9, ToString () and tolocalestring () convert the array to a string

var arr = [1,2,3]

Arr.tostring (); Generating "1,2,3" is the same as a join () that does not use any parameters.

The array method in the second and ECMAScript

1, foreach () foreach () traverses the array from start to finish, calling the specified function for each element.

var arr = [1, 2, 3, 4, 5];
var sum = 0;
Arr.foreach (function (value) {
sum = sum + value;
});
document.write (sum); Sum is finally 15

2 the map () map passes each element of the called array to the specified function and returns an array.

var arr = [1, 2, 3, 4, 5];
var arr1 = Arr.map (function (value) {
return value + 1;
});
document.write (Arr1.join ()); ARR1 for [2,3,4,5,6]

3. Filter () filters, the returned element is a subset of the call array, filtering out elements that do not meet the criteria.

var arr = [1, 2, 3, 4, 5, 6];
var arr1 = arr.filter (function (i) {return i% 2 = 0});
document.write (Arr1.join ()); ARR1 for [2,4,6]

4, every () and some ()

Every () returns true if and only if all elements in the array call the decision function to return true. The first time you return false, you stop the traversal.

Some () when an element in an array calls the decision function to return True, it returns true. The first time you return true, you stop the traversal.

var arr = [1, 2, 3, 4, 5, 6];
var a = Arr.every (function (x) {return x > 3;});
var B = arr.some (function (y) {return y > 3;});
document.write ("A's value is:" + a); The value of a is false,a not all elements are greater than 3
document.write ("B's value is:" + b); The value of B is the presence of an element greater than 3 in True,b

5, reduce () and reduceright ()

Reduce () combines the elements of an array with the specified function to produce a single value, the first parameter is a simplified action function, and the second parameter is the initial value passed to the function. The final result is that the initial value is computed once by the combination function and the final result. The second parameter, the initial value, can be omitted, and the initial value is omitted to be computed directly from the first element.

var arr = [1, 2, 3, 4, 5, 6];
var count = arr.reduce (function (x, y) {return x + y;},0);
document.write (count);
Reduceright (); The only difference to reduce () is that it selects elements from right to left to compute.

6, IndexOf () and Lastinsexof ()

IndexOf () indexOf () returns the index of the first element found at least at the end of the beginning.

LastIndexOf () LastIndexOf () The reverse lookup element returns the index of the first element found.

var arr = [1, 2, 3, 2, 1];
var i = Arr.indexof (2); Search from the beginning, meet 2 for the first time is arr[1], so return 1
var j = Arr.lastindexof (2); Search from tail to head, first met 2 is arr[3], so return 3
document.write (i + "<br/>");
document.write (j);

7, Array.isarray (); Determines whether an object is an array object

var arr = [1, 2, 3];
var str = "STR1";
document.write (Array.isarray (arr)); Returns True arr is an array object
document.write (Array.isarray (str)); Returns False Str is a string, not an array object

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.

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.