Summary of Common Methods of js Array and String
- 2021-07-10 18:47:15
- OfStack
Recently, I have been combing the basics of js, starting with arrays and strings.
Common methods of string:
1. The position truncated by substring (index of the beginning position of start, index of the end position of end) does not contain the character of the end position, and only writes 1 parameter to indicate that it is truncated from the beginning position to the end
var str='abcdefg';
str.substring(1) // Get bcdefg str.substring(1,3) // Get bc
When you enter a negative value, change the negative value to 0, which is the smaller as the starting position
str.substing(-1,1) =
>
str.substring(0,1) //a
str.substring(1,-2) =
>
str.substring(0,1) //a
2. slice (start start position index, end end position index) is basically similar to substring, except that the parameter is negative.
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
Add the value to the length of the string when you enter a negative value
str.slice(-1) =
>
str.slice(6) //g
str.slice(1,-2) =
>
str.slice(1,5) //bcde
str.slice(-2,-1)=
>
str.slice(5,6) //f
Value becomes 0 when its absolute value is greater than the length of the string
str.slice(-22) = > str.substring(0) //abcdefg
Returns ''when the absolute value of the second parameter is greater than the length of the string
3. substr (index of starting position of start, number of characters that end needs to return)
var str='abcdefg';
str.substr(1) //bcdefg str.substr(1,1) //b
When a negative value is entered, the start parameter is added to the length of the string, and when end is negative, the parameter becomes 0
str.substr(-1) =>str.substr(6)//g
str.substr(-2,-3) // ''
4. The charAt (index) method returns the character at the specified index position. If the index value exceeds the valid range (0 and string length minus 1), it returns an empty string.
var str='abcdefg';
str.charAt(2) // c
5. index (string) returns the first occurrence of a substring within an String object. If no substring is found,-1 is returned.
var str='abcdefga' str.indexOf('a') // 0 str.indexOf('h') //-1
6. lastIndexOf (string) flashback lookup
Returns the first occurrence of a substring within an String object. If no substring is found,-1 is returned.
var str='abcdefga' str.lastIndexOf('a') // 7
7. split (str) Splitting strings into arrays with parameters
var str='abcadeafg' str.split('a') //["", "bc", "de", "fg"]
8. The toLowerCase method returns a string in which the letters are converted to lowercase.
9. The toUpperCase method returns a string in which all letters are converted to uppercase.
10. The match () method retrieves a specified value within a string or finds a match for one or more regular expressions
11. The search method returns the position of the first string that matches the regular expression lookup.
12. replace is used to find a string that matches 1 regular expression, and then replaces the match with a new string
http://www.cnblogs.com/bijiapo/p/5451924.html
Common methods of arrays
1. push is added to return the added array at the end
2. unshift added to the front returns the added array
3. shift Delete (from the front) returns the processed array
4. pop Delete the last entry and return the processed array
5. reverse array flipping returns the processed array
6. join Array Converted to String
var arr=[1,2,3,4,5], str=arr.join('--');
console.log(str); // 1--2--3--4--5 With join Cut the array with parameters in the
console.log(arr); // [1,2,3,4,5] The original array remains unchanged
7. slice (start, end) truncates an array from start (beginning) to end (end not included)
Returns a new array, leaving the original array unchanged
var arr=[1,2,3,4,5],new=arr.slice(2,4);
console.log(new); // [3,4]
console.log(arr); // [1,2,3,4,5]
8. concat combinations and
9. splice (start subscript, number, ele1, ele2...) splice array
(1). 1 parameter is truncated from the parameter position and filled in negative numbers similar to the above str slice returns the truncated array change of the original array
var arr=[1,2,3,4,5];
console.log(arr.splice(1)); // [2,3,4,5]
console.log(arr); // [1]
console.lgo(arr.splice(-1)) // [5]
(2). 2 Parameter truncation (starting position, number) returns the truncated array change of the original array
var arr=[1,2,3,4,5];
console.log(arr.splice(1,3)); // [2,3,4]
console.log(arr) // [1,5]
arr.splice(0,1) =>arr.shift()
arr.splcie(arr.length-1,1) =>arr.pop()
(3). Add the original array to add
var arr=[1,2,3,4,5];
console.log(arr.splice(1,0,13)); // []
console.log(arr); // [1,13,2,3,4,5]
(4). Replacement
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
0
10. arr. forEach (item, index, array) {} traverses, loops each similar to jquery
Where the item parameter is the contents of the array, index is its index, and array is the array itself
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
1
There is a problem in nested jump-out loop, which has not been solved for the time being;
11. map method mapping usage is similar to forEach
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
2
12. arr. sort Sequencing
var arr=[1,2,22,11,33,3,5,4];
console.log(arr.sort()) // [1,11,2,22,3,33,4,5]
By default, the sort method is sorted by ascii alphabetical order, not by number size as we think
arr.sort(function(a,b){ return a-b})
a-b from small to large b-a from large to small
13. By the way, write about the sorting methods I know
(1) Bubble sorting compares two adjacent numbers every time. If the last one is smaller than the previous one, change the position
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
4
(2) Quick sort 2 points method, find the middle number, take out (new array), the original array does not, every time compared with this number, small to the left, big to the right
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
5
14. Write down the de-duplication of the array
(1) The double-layer cycle is not very good
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
6
(2) No duplication of key using json
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
7
(3) Sort by sort method, and remove the same items next to it
var arr=[2,3,2,4,4,4,5],
arr2=[];
arr.sort();
for(var i=0;i<arr.length;i++){
if(arr[i]==arr[i+1]){
arr.splice(i--,1);
}
}
1 Some common mathematical methods
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
9
Some Comprehensive Applications of Array and String
1. Intercept suffix names
(1) var str='1.xxx.avi';
str=str.substring(str.lastIndexOf('.')+1);
(2) var str='1.xxx.avi';
var arr=str.split('.');
console.log(arr[arr.length-1]);
2. The letters are flipped and the first letter is capitalized
var str='wo shi yi ge demo',
arr=str.split(' ');
for(var i=0;i<arr.length;i++){
console.log()
arr[i]=arr[i].charAt(0).toUpperCase()+arr[i].substring(1);
}
arr.reverse();
str=arr.join(' ');
3. Statistics of Character Occurrence in str
var str='aaaandkdffsfsdfsfssq12345',
json={},
n= 0,
sName;
for(var i= 0,len=str.length;i<len;i++){
var Letter=str.charAt(i);
// Statistical times
if(json[Letter]){
json[Letter]++;
}else{
json[Letter]=1;
}
}
// Find the biggest
for(var name in json){
if(json[name]>n){
n=json[name];
sName=name;
}
}
console.log(' The letters that appear the most '+sName+' The number of times is '+n);
4. Simple parsing of url parameters
var str='abcdefg';
str.substr(1) //bcdefg str.substr(1,1) //b
3