JavaScript stuff (i) JavaScript array usage summary (2)

Source: Internet
Author: User
Tags javascript array

1, array sorting, the default ascending order, the sort () method invokes the ToString () of each item in the array, and then compares the resulting string to sort//sort (): Returns the array after sorting//var arr = [23,1,3,56,42,-1];// Arr.sort ();//alert (arr); -1,1,3,23,42,56//even though each item in the array is numeric, the sort () method compares the string//var arr = [0,1,5,10,15];//arr.sort ();//alert (arr); 0,1,10,15,5//sort () can receive a function parameter that is used to compare an array item/*function compare (A, B) {if (a < C) {return-1;} else if (a > B) {return 1;} Else{return 0;}} var arr = [0,1,5,10,15];arr.sort (compare); alert (arr); 0,1,5,10,15*///2, inverted array: reverse (), returns the array after sorting//var arr = [0,1,5,10,15];//arr.reverse ();//alert (arr); 15,10,5,1,0//3, Concat () method: Creates a new array based on all items in the current array//concat () creates a copy of the current array, adds the received arguments to the end of the array, and returns a new array. If you do not pass a parameter to the Concat () method, simply copy the current array and return the copy. If one or more arrays are passed to concat (), each item of the array is added to the result array, or the end of the array is simply added if the pass parameter is not an array. var names = ["Zhangsan", "lisi"];//var names2 = Names.concat (' Wangwu ', ' Zhaoliu ', [' xiaoming ', ' Xiangsun ']);//alert ( names); Zhangsan,lisi//alert (Names2); ZHANGSAN,LISI,WANGWU,ZHAOLIU,XIAOMING,XIANGSUN//4, Slice (): Creates a new array based on one or more items of the current array, receives one or two parameters, the starting and ending positions of the item. The slice method does not affect the original array//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi"];//var names2 = Names.slice (1);//var Names3 = Names.slice (1,4 ); Contains the starting position element, which does not contain the end position element//alert (names); Zhangsan,lisi,wangwu,zhaoliu,sunqi//alert (Names2); Lisi,wangwu,zhaoliu,sunqi//alert (NAMES3); lisi,wangwu,zhaoliu//If there are negative numbers in the slice parameter, use the array length plus the number to locate the//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi"];// Alert (Names.slice ( -2,-1)); Slice ( -2,-1) = slice (3,4) returns "Zhaoliu"//If the end position is less than the starting position, an empty array//alert (Names.slice (4,1)) is returned; Empty array//5, Splice () method: Inserts an item into an array, and the splice () method always returns an array containing the items removed from the original array, or an empty array if no items are deleted. (1): Delete array items, where to start deleting, delete several items//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi"];//alert (Names.splice (0,2)) ; Zhangsan,lisi//alert (names); wangwu,zhaoliu,sunqi//(2): Inserts an array item to insert any number of items at the specified location. Parameters: Start position, 0 (number of items to delete), item to insert//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi"];//var deleted = Names.splice (1,0 , ' AA ', ' BB ', ' cc ');//alert (names); Zhangsan,aa,bb,cc,lisi,wangwu,zhaoliu,sunqi//alert (deleted);//Returns an empty array//(3): Replace, to the specified positionInsert any number of items, and delete any number of items at the same time, parameters: Start position, number of items to delete,//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi"];//var deleted = Names.splice (the ' AA ', ' BB ', ' cc ');//alert (names); Zhangsan,aa,bb,cc,wangwu,zhaoliu,sunqi//alert (deleted);//lisi//6, indexOf () \ lastIndexOf (): Returns the position of the item to find in the array, Not found return-1, using congruent = = = comparison//Support browser contains ie9+, other browsers//var names = ["Zhangsan", "Lisi", "Wangwu", "Zhaoliu", "Sunqi", "Lisi", "Wangwu", " Sunqi "];//alert (Names.indexof (" Lisi ")); 1//alert (Names.lastindexof ("Lisi")); 5//7, array traversal: every (), filter (), ForEach (), map (), some () parameter: A function running on each item, the scope object that runs the function//the browser that supports these traversal methods: ie9+ and other Browsers//every (): Iterate through the array each item, if each item returns TRUE, returns True//some (): Iterates through each item of an array, returns True//filter () if any of the entries return true: Filters the values that satisfy the criteria, returns a new array of these values//map (): Returns an array , each entry of this array is the result of running the incoming function on the corresponding item of the original array. ForEach (): No return value, same as for loop/*var numbers = [1,2,3,4,5,4,3,2,1];var Everyresult = Numbers.every (function (Item,index, Array) {return item > 2;});  alert (Everyresult); Falsevar Someresult = numbers.some (function (item,index,array) {return item > 2;}); alert (Someresult);  Truevar Filterresult = numbers.filter (function (item,index,array) {return item > 2;});  alert (Filterresult); 3,4,5,4,3var Mapresult = Numbers.map (function (item,index,array) {return item*2;}); Alert (Mapresult),//2,4,6,8,10,8,6,4,2numbers.foreach (function (Item,index,array) {//todo}), *///8, reduce () \ Reduceright (): Iterates over all the items in the array, constructs a final return value//reduce (): Traversing backward from the front, reduceright (): traversing//receiving four parameters from latter: Previous value, current value, item index, array object// The function return value is automatically passed to the next item as the first argument. Use reduce () to sum var numbers = [1,2,3,4,5,6,7,8,9,10];var sum = numbers.reduce (function (Prev,cur,index,array) {return prev + cur;}); alert (sum); 55

JavaScript stuff (i) JavaScript array usage summary (2)

Related Article

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.