Home > front end >  Js arrays and strings commonly used method
Js arrays and strings commonly used method

Time:10-14

Array is we often use in js, js built many methods about array, here, I to list common array methods:

1. Create an Array using the new Array () method creates an Array: var arr=new Array (); If you know the length of the Array can specify the length of the Array in the new: var arr=new Array (20), but if you write a creates a string of length 1, equal to the string Array, the Array literal variable method is used to create an Array: var arr=[' a ', 'b', 'c'].
2. Use detection array arr. IsArray (arr) tests whether array,

3. Push method Push method is added to the end of the first array element, var arr=[' a ', 'b']. Arr. Push (" v ");//the output of the arr is [' a ', 'b', 'c'].

4. Pop method Pop method is removed at the end of the array elements, and returns the element, the var arr=[' a ', 'b']. Var popArr=arr. Pop ();//popArr output is b, arr output is a push and pop method can be used to achieve stack

5. Will shift shift method removes the first element of the array, and returns the element, the var arr=[' a ', 'b']. Var shiftArr=arr. Shift ();//shift output is a, arr only b elements in the array, using the method of shift and push method can realize the queue

6. Method of unshift unshift method is to add as a forward end length of an array, and returns the length of the array, var arr=[' a ']; Var length=arr. Unshift (' b ', 'c');//output length is 2, the use of unshift and pop can implement a queue in the opposite direction

7. Concat method concat when no parameters will duplicate the current array, return to copy, but have one or more arrays, and will add these to the results in an array, for example: var colors=[' red 'and' blue ']. Colors. Concat (' yellow '[' a']);//the output of the colors is red, blue, yellow, a

8. Slice slice method popular point is to intercept an array from the specified position, and returns the array of interception, not including the end position of the item, for example: var arr=[' a ', 'b', 'c']. Var new=arr. Slice (1);//the new output is b, c, var subStr=arr. Slice (1, 2)//subStr output is b

9. Splice splice method is a powerful array, it can delete data can also be inserted into the data, also can remove and replace the data, in this, I simply introduce the first parameter specifies the delete method to delete the number of the location of the first item and delete var arr=[' a ', 'b', 'c']. Var c=arr. Splice (1, 1);//arr output for a, c

10. The join method join array with a specified character can be wired together, then the output in the form of a string var arr=[1, 2, 3]; The console. The log (arr. Join (' & amp; '));//1 & amp; 2 & amp; 11 3. Create a String 1) through a New String () way of creating, returns a New object (2) to create 3) directly by literal charAt (index) query to specify the index character, if there is a return character, otherwise it returns an empty String (4) the concat (str1... ) STR. Concat (str1) used to connect to one or more strings, 5) indexof (value, the from) the value must be parameters, said the from that position to check, if you don't write the from, the default from the first lookup, return to the position of the search string, if the string does not exist return 1 6) lastIndexOf (vale, the from) from the forward lookup string position, not find return 1, 7) replace (replaceStr, STR) replaceStr said to replace string, or can be a regular expression, behind can replace all of the matched string with g said, generally written/a/g, replace all of the 8) said the split (STR) STR according to the specified character division, said at the time of division '| a | c', for example, he will be divided into [', 'a', 'c'] such an ARRAY, and ARRAY. Join contrary (STR), 9) slice (the from and to) string segmentation, the from and to start and end position respectively, 10) substr (the from, length) string, from the starting position, length said several string segmentation 11) toLowerCase () uppercase to lowercase 12) toUpperCase () lowercase
  • Related