The array method in JS

Source: Internet
Author: User
Tags array length prev

Array methods have array prototype methods, there are methods inherited from object objects, here we only introduce the array of prototype methods, array prototype method mainly has the following:

Join ()
Push () and pop ()
Shift () and unshift ()
Sort ()
Reverse ()
Concat ()
Slice ()
Splice ()
IndexOf () and LastIndexOf () (ES5 new)
ForEach () (ES5 new)
Map () (ES5 new)
Filter () (ES5 new)
Every () (ES5 new)
Some () (ES5 new)
Reduce () and Reduceright () (ES5 new)

1. Join ()

Converts the specified array to a string, the parameter in the method is actually the delimiter that the elements in the array are stitched together into a string, and the parameter ellipsis defaults to the comma as the delimiter.

2. Push () and pop ()

Push: Adds an element to the end of the specified array, can have multiple parameters, returns the length of the array after the addition

Pop (): Deletes the end of the specified array of elements, the pop has no parameters, each call can only delete the end of an element, the return value is the deleted element;

var list=["Apple", "orange", "banana", "watermelon", "cherry";

var count=list.push ("Lychee", "Blueberry");

var list=["Apple", "orange", "banana", "watermelon", "cherry";

Console.log (list)//["Apple", "orange", "banana", "watermelon", "Cherry", "lychee", "Blueberry"

Console.log (count)//7

var a=list.pop ();
Console.log (list); ["Apple", "orange", "banana", "watermelon", "Cherry", "lychee")
Console.log (a); Blueberry

3, Shift () and unshift ()

Shift (): Deletes the first item of the original array, has no parameters, and returns the value of the deleted element, or undefined if the array is empty.
Unshift: Add parameters to the beginning of the original array, you can add multiple parameters, and return the length of the array.

4. Sort ()

Sort (): Arranges array items in ascending order-that is, the smallest value is at the front and the largest value is the last.

5, reverse ()

Reverse (): Reverses the order of the array items.

6, Concat ()

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.

7, 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.

12345678910 vararr = [1,3,5,7,9,11];var arrCopy = arr.slice(1);vararrCopy2 = arr.slice(1,4);vararrCopy3 = arr.slice(1,-2);vararrCopy4 = arr.slice(-4,-1);console.log(arr); //[1, 3, 5, 7, 9, 11](原数组没变)console.log(arrCopy); //[3, 5, 7, 9, 11]console.log(arrCopy2); //[3, 5, 7]console.log(arrCopy3); //[3, 5, 7]console.log(arrCopy4); //[5, 7, 9]

Arrcopy only set a parameter, that is, the starting subscript is 1, so the returned array is subscript 1 (including subscript 1) to start to the end of the array.
The arrCopy2 sets two parameters, returning a sub-array starting with the subscript (including 1) to the terminating subscript (excluding 4).
ArrCopy3 sets two parameters, terminates subscript negative, and, when negative, replaces a negative number with the value of the array length (6) to replace that position, so it is a sub-array starting from 1 to 4 (not included).
Both parameters in ArrCopy4 are negative, so the array length of 6 is converted to positive numbers, so it is equivalent to slice (2,5).

8, Splice ()

Splice (): Very powerful array method, it has many kinds of usages, can implement delete, insert and replace.

1-add element: the first parameter means to add from that subscript position, the second parameter is 0, and the third parameter indicates that the element to be added can have more than one

2. Replace element: The first parameter means to add from that subscript position (including where the subscript starts), the second parameter represents the length of the substitution, and the third parameter indicates what element to replace

3-Delete Element
The first parameter is the subscript position at which to start the deletion, the second parameter is the length of the deletion, and the third parameter does not fill in

9, IndexOf () and LastIndexOf ()

Used to determine if an element exists in the array, if present, returns the subscript of the element, if no return 1 exists, the former is looked up from the array, and the latter is looking forward from the end of the array

10. ForEach ()

ForEach (): Iterates through an array and runs the given function for each item in the array. This method has no return value. Parameters are function types, with the default arguments: The array contents of the traversal, the array index of the content, and the array itself.

vararr = [1, 2, 3, 4, 5];

arr.forEach( function (x, index, a){ console.log(x + ‘|‘ + index + ‘|‘ + (a === arr)); }); // 输出为: // 1|0|true // 2|1|true // 3|2|true // 4|3|true // 5|4|true

11. 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.

12345 vararr = [1, 2, 3, 4, 5];var arr2 = arr.map(function(item){returnitem*item;});console.log(arr2); //[1, 4, 9, 16, 25]

12. Filter ()

Filter (): "Filter" function, each item in the array runs the given function, returning an array that satisfies the filter condition.

12345 vararr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];var arr2 = arr.filter(function(x, index) {returnindex % 3 === 0 || x >= 8;}); console.log(arr2); //[1, 4, 7, 8, 9, 10]

13, every ()

Every (): Determines whether each item in the array satisfies the condition and returns true only if all items satisfy the condition.

vararr = [1, 2, 3, 4, 5];

var arr2 = arr.every( function (x) { return x < 10; }); console.log(arr2); //true

14, some ()

Some (): Determines if there are any items in the array that satisfy the condition, and returns true if one satisfies the condition.

vararr = [1, 2, 3, 4, 5];

var arr2 = arr.some( function (x) { return x < 3; }); console.log(arr2); //true

15. Reduce () and reduceright ()

Both of these methods implement all the items of the iterated group, and then build a value that will eventually be returned. The reduce () method starts from the first item in the array and traverses through to the end. The Reduceright () begins with the last item in the array and traverses forward to the first item.

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. The first iteration occurs on the second item of the array, so the first parameter is the first item of the array, and the second is the second item of the array.

The following code uses reduce () to implement an array summation, and the array begins with an initial value of 10.

varvalues = [1,2,3,4,5];

var sum = values.reduceRight( function (prev, cur, index, array){ return prev + cur; },10);

 

The array method in JS

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.