The array type of the JS reference type

Source: Internet
Author: User

//The 1.concat () method does not change the original array, copying a copy of the original array to splicevararr = [' Red ', ' Zhansan ', ' Xin '];varARR2 = Arr.concat (' 2 ', [' 3 ', ' 9 ']); Console.log (arr); //[' Red ', ' Zhansan ', ' Xin ']Console.log (ARR2);//[' Red ', ' Zhansan ', ' Xin ', ' 2 ', ' 3 ', ' 9 ']//The 2.slice () method does not change the original array, with two parameters, the result of the interception contains the head without the tail, only one parameter, the Intercept is from the parameter specified position to the end of the current array of all itemsvarARR3 = [' sasd ', ' 2 ', ' Zan ', ' Xin '];varARR4 = Arr3.slice (1);varARR5 = Arr3.slice (1, 3); Console.log (ARR4); //[' 2 ', ' Zan ', ' Xin ']Console.log (ARR5);//[' 2 ', ' Zan ']//3.splice () method inserts data into the middle of the arrayvararr6=[1,2,3,4,5]//DeletevarArr7=arr6.splice (0,2)//Delete the first two items of an arrayConsole.log (ARR7)//[1, 2]Console.log (ARR6)//[3, 4, 5] The original array arr6 is changed//Insert//Splice (start position, number of items to be deleted, items to insert, items to insert,...)vararr8=[2,3,4,6]varArr9=arr8.splice (2,0, "red", ' oo ')//the current array position 2 item is deleted, and then the string red and OO are inserted starting at position 2Console.log (ARR9)//Empty ArrayConsole.log (ARR8)//[2, 3, ' red ', ' oo ', 4, 6] Change the original array//Replace//Splice (start position, number of items to be deleted, items to insert, items to insert,...) The inserted item does not have to be equal to the deleted itemvararr10=[1,2, ' A ', ' ASD '];varArr11=arr10.splice (1, 2, "Hello", ' Hai '); Console.log (ARR11)//[2, ']Console.log (ARR10)//[1, ' Hello ', ' hai ', ' asd ']//4. Position Method IndexOf () LastIndexOf () returns 1 when comparing the first parameter to each item of the array, must use the congruent = = =varnumbers=[1,2,3,4,5,6,4,7,8];console.log (Numbers.indexof (4))//location is 3Console.log (Numbers.lastindexof (4))//location is 6//5. Iterative Methodsvarnum=[1,2,3,4,6,4,3,2,1];varResult=num.filter (function(Item,index,arr) {return(Item >2)//values greater than 2 in the array form a new array}) Console.log (Result)//[3, 4, 6, 4, 3]varResult1=num.map (function(Item,index,arr) {return(item*2)//Array Values All * * Create a new array}) Console.log (RESULT1)//[2, 4, 6, 8, A, 8, 6, 4, 2]varResult2=num.foreach (function(Item,index,arr) {Console.log ("Item" +item+ "-index" +index+ "-arr:" +arr)//the array is similar to the For Loop//item1-index0-arr:1,2,3,4,6,4,3,2,1//item2-index1-arr:1,2,3,4,6,4,3,2,1//item3-index2-arr:1,2,3,4,6,4,3,2,1//item4-index3-arr:1,2,3,4,6,4,3,2,1//item6-index4-arr:1,2,3,4,6,4,3,2,1//item4-index5-arr:1,2,3,4,6,4,3,2,1//item3-index6-arr:1,2,3,4,6,4,3,2,1//item2-index7-arr:1,2,3,4,6,4,3,2,1//item1-index8-arr:1,2,3,4,6,4,3,2,1})//6. Merge method The function of reduce () Reduceright () accepts 4 parameters, the previous value, the current value, the index of the item , and the array objectvarval=[1,2,3,4,5,6,7,8,9,10];varSum=val.reduce (function(Prev,cur,index,arr) {returnprev+cur;}) Console.log (sum)// -

The array type of the JS reference type

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.