JavaScript basics (Methods of arrays)

Source: Internet
Author: User
Tags array sort

Array of methods (15)
The
object data type; The array member has a corresponding index length: the number of array members, the operation changes the array of methods, the methods of these arrays are built
-in;
//1. Method effect;//2. Parameters of the method//3. Method return value//4. Whether the original array has changed;1.push
1): Add an entry to the end of the array, 2): The parameter is the new item, you can pass multiple; 3): The number of array members of the new array, 4): The original array has changed;
var ary = [12,89,89,36,0, "DSH",true,];ary.push (console.log) (ary );

2.pop
1): Delete the last item of the array 2): Does not need to pass parameter 3): the deleted Item 4): The original array has changed;
Ary.pop (); Delete ary[ary.length-1]console.log (ary[7]); ary.length-;

3:unshift
1): Add an entry to the beginning of the array, 2): Need to pass parameter 3): The number of array members of the new Array 4): The original array has changed;
Ary.unshift (N); console.log (ary);

4.shift
1): Delete the first item of the array 2): no parameter 3): the deleted Item 4): the original array changes;
Ary.shift ();

5.slice
1): Intercept of the array 2): Slice (m,n): Starts from the array index m, intercepts the index n, but does not contain n; [Before Package]slice (m): Starting from index m, intercept to the end; Slice (): Clone of Array slice (0);//Index negative: Let current length+ negative number; 3): Return value is intercepted array 4): The original array does not change;
var ary = [12,89,89,36,0, "DSH",true,];ary.slice ()//  no reference; copy clone Ary.slice (1)//  start at index 1, intercept to the end of the array ary.slice (2,6)//  start at index 2, intercept to index 8, but does not contain 8;  supports negative numbers //  []

6.splice
1): Delete a few items in the array 2): Splice (m,n): Starting from the index, delete n Splice (m): Delete from index m to the end, splice (0): Splice (m,x,n), replace from index m, delete x, replace with N; 3): Return a value, delete those items, and return 4 in an array): The original array has changed;
Ary.splice (0); // starting at index 0, deleting to the end of the array // starting at index 1, delete 4 items ary.splice (1,3,5,19)//  replace ary.splice (1,0,5)//  Add a number 5 to the front of index 1; console.log (ary);

7.sort
1): Array sort 2): parameter
  • 1.sort (): Only array members are arrays of the same number of digits
  • 2.sort (A, A, b) {return a-B}) sorted from small to large
  • 3.sort (function (A, b) {return b-a}) from 3 to 4): Array after sort: The original array has changed;
Ary.sort (): array with the same number of array member digits; var ary = [12,8,99,67,85,1,35,66, "" "]; var ary = [1,8,5,3,2]; var ary = [123,678,987,567,345,234];ary.sort (function  (A, b) {return A- b;}); var newary = Ary.sort (function  (A, b) {return B-A;}); Console.log (newary);

8.reverse
1): Reverse the array 2): Do not need to pass the parameter 3): array member order upside down after the array 4): the original changes;
Console.log (Ary.reverse ()); Console.log (ary);

9.concat:
1): Array of stitching 2):
  • 1. Do not pass parameters: cloning of arrays
  • 2. Pass parameters, (arrays, each item), stitch the incoming arguments into a new array, 3): New array after stitching 4): The original array does not change;
var ary = [12,8,99,67,66,66,85,1,35,66, "" ","]; var ary1 =[1,2,3]console.log (Ary.concat ([100,200,null])); var a = ary.concat (); Console.log (Ary.concat ());

10.join
1): Concatenate the array members into a string according to a specific character; 2):
  • 1. Do not pass parameters, will be separated by commas by default
  • 2. Pass parameters, (specific characters) 3): string after stitching 4): The original array does not change;
Console.log (Ary.join ("+")); // "12+8+99+67+85+1+35+66+78"Console.log (Ary.join ("")); // "1289967851356678"Console.log (Ary.join ()); // "12,8,99,67,85,1,35,66,78"

11.indexOf
1): detects the index position of the array member in the array for the first time, determines whether the current item exists in the array, or returns -1;2 if it does not exist: Requires parameter 3): Returns the first occurrence of the index in the array; 4): The original array does not change;
Console.log (Ary.indexof),Console.log (Ary.indexof (666));Console.log (Ary.indexof (888));

12.lastIndexOf
1): detects the last index position of the array member in the array, determines whether the current item exists in the array, or returns -1;2 if it does not exist: Requires parameter 3): Returns the last occurrence of the index in the array; 4): The original array does not change;
Console.log (Ary.lastindexof (66));

Map
1): Traversing array and Mapping 2): Requires parameter 3): Array after mapping 4): The original array does not change;
var ary = [12,8,99,67,66,66,85,1, "" ","]; var NULL ; var mn = Ary.map (function  (item,index) {Console.log (item); // iterating over an array; The first parameter is the array member Console.log (index); // an index that represents an array member return "<li></li>>"; // sum Total + = number(item); Console.log (total); Console.log (MN);

14.forEach
1): traversal array; no return value; 2): Requires parameter 3): The return value is UNDEFINED4): The original array does not change;
var bhh = Ary.foreach (function  (item,index) {Console.log (item); return ;}) Console.log (BHH); // undefinedconsole.log (ary);

15.toString
1): Turn to String 2): No parameter 3 is required: Returns a string that goes after the brackets 4): The original array is unchanged;
var ary = [1,23,8,9,90]console.log (ary.tostring ()); // "1,23,8,9,90"console.log ([12].tostring ()); // "console.log" ([].tostring ()); // "console.log (ary);

JavaScript basics (Methods of arrays)

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.