JS Array Summary

Source: Internet
Author: User

comparison of data with other languagessame point: Ordered listdifferent points: Each item of the JS array can hold any type of data, and the size of the array can be dynamically adjustedtwo ways to create arrays1) var colors = new Array ();//Create an empty arrayvar colors = new Array (3);//Create an array containing 3 itemsvar colors = new Array ("Red", "green", "blue");//Create an array with 3 strings2) var names = [];var names = ["Red", "green", "Blue"];//names[2]third, the length property of the array1) Detecting the number of items in an array2) can be setvar names = ["Red", "green", "Blue"];names.length = 4;alert (names[3]);//undefined           #######################names.length = 2;alert (names[2]);//undefined         #######################names[names.length]= "BLACK";alert (names);//red,green,blue,black          #######################names[99]= "Yellow";alert (names.length);//100four, array method1) Array method detectionArray.isarray ()2) Conversion methodtoString (): Returns a comma-delimited string of strings of each value in the array;valueOf (): Returns an array that returns the object type of a numeric typejoin (): accepts a parameter that has this character to concatenate the number of characters into a string3) Stack method (LIFO) advanced back outpush (): Receives any number of arguments, added to the end of the array, and returns the length of the modified arraypop (): Removes the last item from the end of the array, returning the removed item4) Queue method (FIFO) FIFO(1) push (): Ibid .shift (): Moves the first item of the divisor group and returns the item(2) Unshift (): Add any item to the front of the array and return the new array lengthpop (): ibid .5) Reorder Methodreverse (): Invert array Itemssort (): By default in ascending order, call the ToString () method of each array item, and then compare the resulting string, sort () to receive a comparison parameter, return the array6) Operation Method(1) concat (): A copy of the current array is created first, and the received parameter is one or more arrays, each of which is added to the result arrayvar names = ["Red", "green", "Blue"];var names1 = Names.concat ("Brown", ["Black", "Dede"]);(2) Slice ():var names = ["Red", "green", "Blue"];var names1 = names.slice (1);//green,bluevar = names.slice (names2);//green,bluedeleted: Slice (0,2)insert: Slice (2,0, "Red", "FRR");//insert starting from position 2Replace: Slice (2,1, "Red", "FRR")//delete the entry for position 2, and then insert two7) Location method (both can receive two parameters, to find the subparagraphs (optional) represents the index of the location of the lookup start)indexOf (): Looks backwards from the beginning of the array and returns the position of the item to find in the arraylastIndexOf (): Look forward from the end of the array, returning the position of the item to find in the array8) Iterative approachevery (): Each item of an array runs the given function, and returns TRUE if the function returns true for each item .filter (): Returns the array filtered by the given functionForEach (): Just run the given function for each item in the array, no return valuemap (): Each item of an array runs the given function, returning the arrays that comprise the result of the function runsome (): Runs the given function for each item of an array, and returns True if the function returns true for an item9) Merge methodreduce (): Here and Python's list method is particularly like, its own understanding, will be based on each of the functions called, iterate the algebraic group of each item, and then return a final resultreduceright (): With reduce, just start iterating from the last item in the arrayFive: Interview frequently test the array of questions

JS Array Summary

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.