Array of methods (join, intercept, delete, insert, replace, and encapsulate a function)

Source: Internet
Author: User
Tags array length

Connect two arrays; concat to form a new arrayarray 1.concat (array 2, array 1)return value: Array
var arr1=[1,2],arr2=[3,4],arr3;arr3=arr1.concat (ARR2,ARR1); Console.log (ARR3);

Another way to intercept "is come to be"__.slice (start,end)

___.slice (start,end) function: Returns the selected element from an existing arrayParameter if the parameter has a negative number, then use the array length plus the numbers to determine the corresponding position start (must) specify where to begin the selection, if starting from the beginning, select the index is 0, if negative, from the end of the array to calculate the end (optional) specify where to close the selection, is the array subscript at the end of the array fragment (index-1), if not, the segmented array contains all the elements from start to the end of the array. return value: Array
var colors=["Red", "green", "blue",yellow]; var newcolors=colors.slice (1,3); Console.log (newcolors);

Face Test
// complete the code and implement the B array copy of the A array, var a=[1, "Yew", 3],b; // 1 array traversal, pushB =new  Array ();  for (var i=0;i<a.length;i++) {B.push (a[i]); // 2concat ()b=[].concat (a); 3slice (); b=a.slice (0); Console.log (b);

Splice, "The injection of the shop Lai"1 DeleteSyntax ___.splice (index,count), function: Deletes 0 or more elements from index. Return value: Contains an array of elements that have been deleted. Description: Count is the number of items to be deleted, and if set to 0, the item is not deleted. If not set, all values starting with index are deleted.2 insertingSyntax ___.splice (index,0,item1,.... itemx) function: Insert value parameter at specified position: index: Start position (index + 1) 0: Number of items to delete item1 ... ITEMX: Return value of item to insert: array3 Replacement——. Splice (INDEX,COUNT,ITEM1....ITEMX) function: Inserts a value at the specified position and deletes any number of item parameters at the same time: index: Start position count: Number of items to delete item1 .... ITEMX: The item to be inserted returns a value: An item that is deleted from the original array (an empty array is returned if no items are deleted)
var arr=["A", "B", "C", "D"]1//  var add=arr.splice (2,2);//Output Delete c,d2// var add=arr.splice (3,0, "M", "n");//Because it is not deleted, so it is empty, and ARR shows insert completion.  var// index 1, delete two. Add=bc,arr Insert Complete console.log (arr); Console.log (add);

Method of LocationGrammar:__.indexof (Searchvalue,startindex)Function: Looks backwards from the beginning of the array (position 0). Parameters: Searchvalue: Must be, the item to find; StartIndex: Optional, the index of the start position. Return value: Number, find the position of the item in the array, and return-1 without finding the case.
var nums=[1,4,7,5,7,2,3,4,]; var // Detection 7 begins at position 3 bits in the array, var // First Console.log (POS);

___.lastindexof (Searchvalue,startindex)

Function: Looks forward from the end of the array. Index from the front. Parameters: Searchvalue: Must be, the item to find; StartIndex: Optional, the index of the start position. Return value: Number, find the position of the item in the array, and return-1 without finding the case. ... The following are compatibility issues. 1 when comparing the first argument to each item in the array, the strict equality operator is used, that is, the items required for the lookup must be strictly equal. 2 The location of the array is ECMAScript5 to the number of instances, so the browser is supported by only Ie9+,firefox 2+,safari3+,opera9.5 and chrome creating function functions
//encapsulates a method that implements IndexOf functions. varnums=[1,4,6,2,3,5,8,9,7];functionArrayindexof (arr,value) { for(vari=0;i<arr.length;i++) {//To detect where value appears in Arrif(arr[i]===value) {returnI//find the corresponding number, for the number of indexes I. }}return-1;//otherwise appears-1}varPos2=arrayindexof (nums,4);//string to convertConsole.log (POS2);

Array of methods (join, intercept, delete, insert, replace, and encapsulate a function)

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.