javascript--Arrays Common Array Method summary

Source: Internet
Author: User

1. What are the method functions?
2. What are the parameters passed?
3. What is the return value?
4. Whether the original array has changed
/* First group: About array additions, deletions, and modifications
1.push adds new content to the end of the array, returns the length of the new array that was added, and changes the original array
var arr=[10,11,12,13,14,15];
var res=arr.push (16,17);
Console.log (RES); 8
2.unshift adds new content to the beginning of the array, returns the length of the new array after the addition, and changes the original array
var res=arr.unshift (16,17);
Console.log (RES); 8
3.splice (n,0,x) adds a new content to the middle of the array starting at index N, deleting 0 content and placing the new X in front of index N.
An empty array is returned, the original array is changed
N-Starting Index
Splice (n,m) deletes an array-specified item, starting with N (containing n), deleting the M-element backward, returning the deleted content as a new array, changing the original array
Splice (N,M,X) replaces some of the items in the original array. (delete first, then replace with X). Start with n (containing n), remove m elements backwards, replace with X,
Returns the deleted array, changing the original array
Rule: Splice (0,0,x) is equivalent to Unshift
Splice (arr.length,0,x) is equivalent to push
var res=arr.splice (2,0, "Michael"); Add "Michael" after 12
Console.log (arr);
4.pop Delete the last array, return the deleted item, change the original array
Shift deletes the first array, returns the deleted item, and changes the original array

*/


/*
Second group: Querying and copying of arrays
6.slice (n,m) finds index m (without m) starting at index n (contains n). Returning the found content as a new array, the original array is unchanged.
var arr=[10,11,12,13,14,15];
var res=arr.slice (1,4);
Console.log (RES); [11, 12, 13]
Console.log (arr); [10, 11, 12, 13, 14, 15]
Slice (n) finds the end from index n (contains N)
Slice (0)/slice () copies a copy of the original array intact, array clone
7.concat can also be used to absorb comfortable clones, the original array also changed (equivalent to slice (0))
Concat is intended to implement the concatenation of the array Arr1.concat (ARR2) merges the array arr2 and arrays arr1 into a new array, and the original array does not change
var arr1=[10,11,12,13,14,15];
var arr2=[16,17];
var res=arr1.concat (ARR2);
Console.log (ARR1); [10, 11, 12, 13, 14, 15]
Console.log (RES); [10, 11, 12, 13, 14, 15, 16, 17]

*/
/*
Group Three: Converting an array to our string
8.toString take each item in the array, separated by commas, to form a string, the original array unchanged
var arr=["name", "Michael", "Age", "24"];
var res=arr.tostring ();
Console.log (RES); name,michael,age,24
Console.log (arr); ["Name", "Michael", "Age", "24"]
9.join (delimiter) takes each item in the array, separated by the specified delimiter, and the original array is unchanged.
var arr=["name", "Michael", "Age", "24"];
var res=arr.join ("|");
Console.log (RES); "Name|michael|age|24"
Console.log (res.length); 19
Console.log (arr); ["Name", "Michael", "Age", "24"]
Console.log (arr.length); 4


Implementing the summation of numbers in an array
var arr=[10,11,12,13,14,15];
var str=arr.join ("+"); "10+11+12+13+14+15"
var total=eval (str); Eval converts the specified string into a true frontal expression execution
Console.log (total); 75
Console.log (arr); [10, 11, 12, 13, 14, 15]

*/
/*
Group fourth: Arranging and sorting
The 10.reverse array is inverted and the original array is changed.
var arr=[10,11,12,13,14,15];
Console.log (arr); [10, 11, 12, 13, 14, 15]
var res=arr.reverse (); "10+11+12+13+14+15"
Console.log (RES); [15, 14, 13, 12, 11, 10]
The sorting of 11.sort arrays can be achieved from large to small (from small to large), the original array is also changed
Direct write sort can only handle 10 of the number of orders, processing more than 10 we need to pass a parameter, this parameter must be a function
var arr=[10,12,11,19,13,15,6];
Console.log (arr); [10, 12, 11, 19, 13, 15,6]
var res=arr.sort ();
Console.log (RES); [10, 11, 12, 13, 15, 19, 6]
Improved:
var arr=[10,12,11,19,13,15,6];
Console.log (arr); [10, 12, 11, 19, 13, 15,6]
var res1=arr.sort (function (A, b) {return-A;}); Implemented from small to large
Console.log (RES1);
var res2=arr.sort (function (A, b) {return b-a;}) Implemented from large to small
Console.log (Res2); [10, 11, 12, 13, 15, 19, 6]
var arr=[10,12,11,19,13,6];
Console.log (arr); [10, 12, 11, 19, 13, 15,6]

var res1=arr.sort (function (A, b) {
A represents the current item at the time of each loop, and B is the following item
return a-B; the current item minus the next item, if greater than 0, represents the previous larger than the back, so that the position is exchanged
Bubble sort: Sort implements sorting, which is implemented by the idea of bubbling sort
Console.log (A + "<====>" +b);
return a-B;}); Implemented from small to large
Console.log (RES1);
*/
/*
Group Fifth: some common but incompatible
12.indexOf
Array1.indexof (searchelement[, FromIndex])
Fromindex can be used to start searching the array index. If FromIndex is omitted, the search starts at index 0.
The index of the first occurrence of the searchelement in the array, or 1 if no searchelement is found.
var arr=["Hello", "Michael", "Good", "study"];
var p=arr.indexof ("Mi", 0);
Console.log (P); -1
13.forEach
Array1.foreach (callbackfn[, Thisarg])
CALLBACKFN must be selected. A function that can accept up to three parameters. For each element in the array, ForEach invokes the CALLBACKFN function one time.
Thisarg is optional. The This keyword in the CALLBACKFN function can refer to the object. If Thisarg is omitted, undefined is used as the this value.
If the CALLBACKFN parameter is not a function object, a TypeError exception is thrown.
For each element that appears in the array, the ForEach method invokes the CALLBACKFN function once (in ascending index order). The callback function will not be called for missing elements in the array.
In addition to array objects, the ForEach method can be used by any object that has a length property and has a property name indexed by number.
callback function syntax
The syntax for the callback function is as follows:
function Callbackfn (value, index, array1)
Value of the array element of value.
The numeric index of the index array element.
Array1 the array object that contains the element.
Create an array.
var numbers = [10, 11, 12];

Call the Addnumber callback function for each array element.
var sum = 0;
Numbers.foreach (
function Addnumber (value) {sum + = value;}
);

document.write (sum);
Output:33
https://msdn.microsoft.com/zh-cn/library/ff679980 (v=vs.94). aspx
14.map
Array1.map (callbackfn[, Thisarg])
CALLBACKFN must be selected. A function that can accept up to three parameters. For each element in the array, the map method calls the CALLBACKFN function one time.
[, Thisarg] is optional. The This keyword in the CALLBACKFN function can refer to the object. If Thisarg is omitted, undefined is used as the this value.

return value
A new array in which each element is the return value of the callback function for the associated original array element.

Abnormal
If the CALLBACKFN parameter is not a function object, a TypeError exception is thrown.

Note
For each element in the array, the map method calls the CALLBACKFN function once (in ascending index order). The callback function will not be called for missing elements in the array.
In addition to array objects, the map method can be used by any object that has a length property and has a property name indexed by number.
callback function syntax
The syntax for the callback function is as follows:
function Callbackfn (value, index, array1)
You can declare a callback function with up to three parameters.
Callback parameter definition
Value of the array element of value.
The numeric index of the index array element.
Array1 the array object that contains the element.
Define the callback function.
function Areaofcircle (RADIUS) {
var area = Math.PI * (RADIUS * radius);
Return area.tofixed (0);
}

Create an array.
var radii = [10, 20, 30];

Get the areas from the radii.
var areas = Radii.map (areaofcircle);

document.write (areas);

Output:
314,1257,2827
*/

javascript--Arrays Common Array Method summary

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.