Common methods of string
- charAt() gets the character at the specified subscript
let str = '12345'
console.log(str.charAt(0));//1
- charCodeAt gets the Unicode code of the subscript character
- Unicode assigns a number to all characters to represent that character
let str = '12345'
console.log(str.charCodeAt(1));//The result is 44
- substring returns characters according to the subscript range
let str = '12345'
console.log(str.substring(0, 2));//12
- slice returns a segment of the specified string according to the subscript. It intercepts the previous one of the second subscript parameters. If only one subscript is filled in, it intercepts the last one by default
let str = '12345'
console.log(str.slice(-3));// 345 returns the last three characters
console.log(str.slice(0, 4));//1234 returns all characters from subscripts 0-4
- indexOf finds the location of the first occurrence according to the subscript
let str = '12345'
console.log(str.indexOf(1));//0
- split splits the string into substrings according to the string and becomes an array
let str1 = '123,456'
console.log(str1.split(','));// ['123','456']
- replace full text character replacement
let str = '12345'
console.log(str.replace('1', '9'));//92345 is preceded by the old value and followed by the new value to be replaced
- toLowerCase converts all string letters to lowercase
let str2 = 'LOL'
console.log(str2.toLowerCase());//lol
- toUpperCase converts all string letters to uppercase
let str3 = 'lol'
console.log(str3.toUpperCase());//LOL
Here are the common methods of array
- join converts all elements in the array into strings and connects them together with connectors. If the parentheses are empty, the default is comma "," and
let arr = ['Hello', 'world']
console.log(arr.join());//'Hello','world'
- reverse reverses the order of all elements in the array
let arr = ['Hello', 'world']
console.log(arr.reverse());//['world', 'Hello']
let arr1 = [1, 2, 3, 5, 6, 4]
arr1.sort((a, b) => {
return a - b
})
console.log(arr1);//1,2,3,4,5,6 B-A is in descending order
- concat creates and returns a new array
let arr1 = [1, 2, 3, 5, 6, 4]
console.log(arr1.concat([7, 8]));//1~8
- slice returns a fragment or subarray of the specified array according to the subscript. It intercepts the previous one of the second subscript parameter. If only one subscript is filled in, it intercepts the last one by default
let arr1 = [1, 2, 3, 5, 6, 4]
console.log(arr1.slice(0, 5));//1~5
- splice deletes or inserts elements into the array according to the subscript parameters. The first parameter is the subscript and the second is the specified number. If only one subscript is filled in, it defaults to the last one
let arr2 = [1, 2, 3, 4, 5]
console.log(arr2.splice(0, 1));//[1]
console.log(arr2.splice(2));//[4,5]
- pop deletes the last element at the end of the array and returns the deleted value
let arr3 = [1, 2, 3, 4]
console.log(arr3.pop());//4
- push adds one or more elements at the end of the array and returns the new length of the array
let arr4 = [1, 2, 3]
console.log(arr4.push(4, 5));//5
- shift deletes the last element of the array header and returns the deleted value
let arr4 = [1, 2, 3,]
console.log(arr4.shift());//1
- unshift adds one or more elements to the array header and returns the new length of the array. When there are multiple parameters, the parameters are inserted at one time, and the order of inserting elements remains unchanged
let arrayes = [1,2,3,4,5]
console.log(arrayes .unshift([1, 2]));// 6
console.log(arrayes .unshift(1, 2));// 8
- toString converts all elements in the array into strings and connects them with commas, which is the same as the string returned by the join method without any parameters
let stringse= [1,2,3,4,56]
console.log(stringse.toString());//1,2,3,4,56
- Localized version of toLocaleString toString (connector can be customized)
let arr5 = new Date()
console.log(arr5.toLocaleString());//2021 / 10 / 11 2:21:02 PM
- forEach traverses the array and cannot terminate the traversal before all elements are passed to the calling function. The for loop can use break and has no return value
let arr6 = [1, 2, 3]
arr6.forEach(function (item, index, err) { //Formal parameter: the first value, the second index, and the third entire array
console.log(item, index)
console.log(err) //Entire array
})
- es6 abbreviation - replace function() {} with (parameter) = > {}
let arr6 = [1, 2, 3]
arr6.forEach((item1, index1) => {
console.log(item1, index1)// The first is a value and the second is a subscript
})
- map can also loop through the array and return a new array composed of functions. More are used to re operate and assign values to arrays or objects. We can operate on arrays to obtain a field in all array objects, such as city
let list = [
{
"name": "Flower pain",
"city": "Shenzhen",
}, {
"name": "Jack Ma",
"city": "Hangzhou",
}, {
"name": "Jianlin",
"city": "Wanda",
}
]
var newErr = list.map(item => {
return item.city
})
console.log(newErr)// Shenzhen, Hangzhou, Wanda
- Filter filtering method. In Vue, there are also filters: {} local filtering and global filtering Vue.filter
For example, we can filter the mailbox as nz@qq.com
var person = ["zn@qq.com", "nz@qq.com", "zi@qq.com"];
var newPerson = person.filter(item => {
console.log(item) //Array, here is the string method
return item.indexOf('nz') != -1
})
console.log(newPerson)
- indexOf searches the entire array for elements with a given value, returns the index of the first element, and returns - 1 if it cannot be found
let sun = [0, 1, 2, 3]
console.log(sun.indexOf(4));//-1
- lastIndexOf() indexOf is a start-to-end lookup, while lastIndexOf is a reverse lookup
let sun = [0, 1, 2, 3]
console.log(sun.lastIndexOf(1));
- Every element in every array meets the set conditions and returns true. Otherwise, it returns false. It is equivalent to logic and & &, the same truth is true, the same false is false, and the same false is false.
var inputs3 = document.querySelectorAll('input');
var iptEvery = [...inputs3].every(item => {
return item.checked
})
console.log(iptEvery) //False returns only true/false.
- There is one element in some array, one item is true, and true is returned. Equivalent to logic or 𞓜 the same truth is true, the same false is false, and the same truth is true.
var inputSome = [...inputs3].some(item => {
return item.checked
})
console.log(inputSome);//true
- Array merging method reduce(function(prev,cur,index,array) {}, initial value passed to the function [optional])
Parameters: previous value, current value, index, entire array. This method starts from the first item of the array and traverses one by one to the last item.
Application scenario:
Array summation
var err1 = [8, 2, 3, 4];
var res = err1.reduce((prev, cur) => {
console.log(prev, cur) //8 2 ,10 3,13 4
return prev + cur
}, 0)
console.log(res) //17 final and
- Flat array flattening in es6
let num = [[0, 1], [2, 3], [4, 5]]
console.log(num.flat());//[0,1,2,3,4,5]
- With Infinity, it can be expanded in any number of layers
let num1 = [[0, 1], [2, 3], [4, [5, 6, 7]]]
console.log(num1.flat(Infinity)); //[0,1,2,3,4,5,6,7]
- includes determines whether the array is a character containing the specified value and returns a Boolean
let num2 = [1, 2, 3, 4]
console.log(num2.includes(1));//true
console.log(num2.includes(5));//false