Common methods for "javascript" javascript arrays

Source: Internet
Author: User

Tag: CER numeric copy plain copy blog is not built using arrays

Common methods for JavaScript arrays

One, array creation

1. (1) Create an array using array:

1 var arr1 = new Array ();//Create an empty array 2 var arr2 = new Array (10);//Create an array with 10 items 3 var arr3 = new Array ("A", "B", "C"); Create an array with 3 strings

(2) using the array literal notation:

1 var arr4 = [];//Create an empty array 2 var arr5 = [1];//Create an array with 1 items 3 var arr6 = ["A", "B", "C"]; Create an array with 3 strings

2. modifying array element values

(1) directly to the corresponding elements of the subscript to modify:

1 var arr = ["A", "B", "C"];//Create an array with 3 strings 2  3

(2) According to the current length of the array to modify or delete:

1 var arr = ["A", "B", "C"];//Create an array with 3 strings 2 Arr[arr.length] = "D";//Add a "D" at subscript 3 (that is, the tail of the array) 3 arr.length = arr.length-1; Delete the last item of the array

Second, the common array method

Filter () "Filter" function, each item in the array runs the given function, returning an array that satisfies the filter condition. arr.filter(function(x, index) {return;})
Every () Determines whether each item in the array satisfies the condition and returns true only if all items satisfy the condition. arr.every(function(x) {})
ForEach () Iterates through the array, running the given function for each item in the array. This method has no return value. Parameters are function types, the default is the parameter, the parameters are: traversed array contents, the corresponding array index, the array itself. arr.forEach(function(x, index, a){})
IndexOf () and LastIndexOf () Receives two parameters: the subparagraphs (optional) to find indicates the index at which to find the starting point. Search backwards from the beginning of the array (position 0) or at the end. arr.indexOf(5,2))arr.lastIndexOf(5,4)
Concat () Adds a parameter to the original array. This method creates a copy of the current array, adds the received parameters to the end of the copy, and finally returns the newly constructed array. Without passing arguments to the concat () method, it simply copies the current array and returns a copy. vararr = [1,3,5,7];vararrCopy = arr.concat(9,[11,13]);//[1,3,5,7,9,11,13]
Join () Sets the element group of an array as a string, separator as a delimiter, or by default with a comma delimiter, which receives only one parameter: the delimiter. arr.join("-");
Push () and pop () Push (): Can receive any number of parameters, add them to the end of the array one by one, and return the length of the modified array. Pop (): Removes the last item at the end of the array, reduces the length value of the array, and then returns the item that was removed. Similar to stacks.

Arr.push (item);

var item=arr.pop ();

Shift () and unshift () Shift (): Deletes the first item of the original array and returns the value of the deleted element, or returns undefined if the array is empty.
Unshift: Adds a parameter to the beginning of the original array and returns the length of the array. Similar to queues.

Arr.unshift (,)

var item=arr.shift ();

Sort () Arranges array items in ascending order-that is, the smallest value is at the front and the largest value is the last (default). When sorting, the sort () method invokes the ToString () transformation method for each array item, and then compares the resulting string to determine how to sort. The sort () method compares strings, even if each item in the array is a numeric value. Arr2.sort (function () {});
Reverse () Reverse (): Reverses the order of the array items. Arr.reverse ();
Slice () Slice (): Returns a new array that consists of the entries from the original array that specify the starting subscript to the end subscript. The slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item. In the case of only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array. If there are two parameters, the method returns the item between the start and end positions--but not the end position. Arr.slice (1,4);
Splice () Splice (): Implements Delete, insert, and replace.

Arr.splice (0,2);//delete items No. 0 and 1th

Arr.splice (2,0,4,6);//Insert two elements 4 and 6 in the position of item 2nd

Map ()

Map (): Refers to "Map", which runs the given function for each item in the array, and returns a list of the results of each function call.

The following code uses the map method to achieve the square of each number in the array.

arr.map(function(item){return;})
Some () Some (): Determines if there are any items in the array that satisfy the condition, and returns true if one satisfies the condition. arr.some(function(item){return;})
Reduce () and reduceright () Implement all the items of the iteration group, and then build a value that is ultimately returned. (Go back and back forward). Both methods receive two parameters: a function that is called on each item and (optionally) the initial value that is the base of the merge. The functions passed to reduce () and Reduceright () receive 4 parameters: the previous value, the current value, the index of the item, and the array object. Any value returned by this function will be automatically passed to the next item as the first argument. varsum = values.reduceRight(function(prev, cur, index, array){returnprev + cur;},0);//对values数组求和

Instance:

1 to operate on an array of content=[12,65,92,71,30,6,12,65]: 2 (1) array , descending,3 (2) add 62,63,64 after element=65;4 (3) de-weight

Source:

1 <Script>2 varcontent=[ A, $, the, in, -,6, A, $];3 //Descending arrangement4 functionCompare (value1,value2) {5     returnvalue2-value1;6 }7 Content.sort (compare);8 9 //Add a 62,63,64 after specifying the first element 65Ten varIndex=Content.indexof ( $) One if(Index>=0){ A Content.splice (Index+1,0, +, the, -); - } -  the //Go heavy - Content.sort (compare); - functionRemove (arr) { -     varret=[arr[0]]; +      for(varI=1; I<Arr.length;i++){ -         if(Ret[ret.length-1]!=Arr[i]) { + Ret.push (Arr[i]); A         } at     } -     returnret; - } - content=Remove (content); - alert (content); - </Script>

Common methods for "javascript" javascript arrays

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.