Javascript (1) Summary of javascript Array usage (2), javascript Array

Source: Internet
Author: User
Tags javascript array

Javascript (1) Summary of javascript Array usage (2), javascript Array

// 1. array sorting. The default value is ascending. The sort () method calls the toString () of each item in the array, and then compares the obtained string to sort the result. // sort (): returns the sorted array // var arr = [23,1, 3,56, 42,-1]; // arr. sort (); // alert (arr); //-, // even if each item in the array is a numerical value, sort () the method is also a string // var arr = [0, 1, 5, 10, 15]; // arr. sort (); // alert (arr); //, 5 // sort () can receive a function parameter, used to compare array items/* function compare (a, B) {if (a <B) {return-1;} else if (a> B) {return 1 ;} else {return 0 ;}}var arr = [0, 1, 5, 10, 15]; arr. sort (compa Re); alert (arr); // 0, 1, 5, 10, 15 * // 2. reverse array: reverse (), returns the sorted array // var arr = [0, 1, 5, 10, 15]; // arr. reverse (); // alert (arr); // 15, 10, 5, 1, 0 // 3, concat () method: create a new array // concat () based on all the items in the current array, first create a copy of the current array, and then add the received parameters to the end of the array, then return the new array. // If a parameter is not passed to the concat () method, the current array is copied and a copy is returned. // If one or more arrays are passed to concat (), each item of the array is added to the result array. // If the passed parameter is not an array, the end of the array is simply added. // 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 of the current arrays and receives one or two parameters, the start position and end position of an 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 (); // contains the starting position element, excluding the Ending position element // alert (names); // zhangsan, lisi, wangwu, zhaoliu, sunqi // alert (names2); // lisi, wangwu, zhaoliu, sunqi // alert (names3); // lisi, wangwu, zhaoliu // If the slice parameter has a negative number, add the number to the length of the array to locate/var names = ["zhangsan", "lisi", "wangwu", "zhaoliu", "sunqi"]; // alert (names. sli Ce (-2,-1); // slice (-2,-1) = slice (3, 4) returns "zhaoliu" // if the end position is smaller than the start position, returns an empty array // alert (names. slice (); // empty array // 5. splice () method: insert an entry into the array. The splice () method always returns an array, this array contains items deleted from the original array. // if no items are deleted, an empty array is returned. // (1): Delete the array item, and delete the items starting from/var names = ["zhangsan", "lisi", "wangwu ", "zhaoliu", "sunqi"]; // alert (names. splice (); // zhangsan, lisi // alert (names); // wangwu, zhaoliu, sunqi // (2): insert an array entry, insert any number of items to a specified position. Parameter: Start position, 0 (number of items to be deleted), item to be inserted // var names = ["zhangsan", "lisi", "wangwu", "zhaoliu ", "sunqi"]; // var deleted = names. splice (, 'A', 'bb', 'cc'); // alert (names); // zhangsan, aa, bb, cc, lisi, wangwu, zhaoliu, sunqi // alert (deleted); // returns an empty array // (3): replace, insert any number of items to the specified position, and delete any number of items at the same time. parameter: start position, number of items to be deleted), items to be inserted // var names = ["zhangsan", "lisi", "wangwu", "zhaoliu", "sunqi"]; // var deleted = names. splice (, 'A', 'bb', 'cc'); // alert (names ); // Zhangsan, aa, bb, cc, wangwu, zhaoliu, sunqi // alert (deleted); // lisi // 6, indexOf () \ lastIndexOf (): returns the position of the item to be searched in the array. If no position is found,-1 is returned, comparison with full equal ==/// supported browsers include IE9 + and 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 () parameters: the function running on each item runs the function's scope object // browser that supports these traversal methods: IE9 + and Other browsers // every (): traverses each item of the array. If each item returns true, true is returned. // some (): traverses each item of the array. If any item returns true, returns true // filter (): filters values that meet the conditions, returns a new array composed of these values // map (): returns an array, each item of this array is the result of running the input function on the corresponding item of the original array. // ForEach (): no return value, same as for loop/* var numbers = [1, 2, 3, 4, 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); //, 3var mapResult = numb Ers. 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 (): traverses all the items in the array, construct a final return value // reduce (): traverse from the front to the back, cerceright (): traverse from the back. // receive four parameters: previous value, current value, and item index, the array object // function return value is automatically passed to the next item as the first parameter. // Use reduce () to sum var numbers = [1, 3, 4, 5, 6, 7, 8, 9, 10]; var sum = numbers. reduce (function (prev, cur, index, array) {return prev + cur;}); alert (sum); // 55

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.