Array and string methods that must be remembered

Keywords: Javascript

array

1. concat() is used to connect two or more arrays and return a new array (without changing the original array)

let arr1=[1,2,3,4]
let arr2=[5,6]
console.log(arr1.concat(arr2))//[1,2,3,4,5,6]

2. pop() is used to delete and return the last element of the array (changing the original array)

let arr=[1,2,3,4]
let res=arr.pop()
console.log(res)//4
console.log(arr)//[1,2,3]

3. push() is used to add one or more elements to the end of the array and return a new length (changing the original array)

let arr=[1,2,3,4]
let len=arr.push(88)
console.log(len)//5
console.log(arr)//[1,2,3,4,88]

4. shift() is used to delete the first element of the array from it and return the value of the first element (changing the original array)

let arr=[1,2,3,4]
let res=arr.shift()
console.log(res)//1
console.log(arr)//[2,3,4]

5. unshift() is used to add one or more elements to the beginning of the array and return a new length (changing the original array)

let arr=[1,2,3,4]
let len=arr.unshift(6)
console.log(len)//5
console.log(arr)//[6,1,2,3,4]

6. indexOf(item,start) is used to return the position of a specified element in the array (without changing the original array)

The parameter start (optional) specifies where to start the retrieval

Returns - 1 if the specified element is not found in the array

let arr=[1,2,3,4]
let site=arr.indexOf(3)
let site2=arr.indexOf(3,3)
console.log(site)//2
console.log(site2)//-1
console.log(arr)//[1,2,3,4]

7. slice(start,end) is used to return the selected element from the existing array and a new array (without changing the original array)

The parameter end (optional) specifies where to end the selection

Results left closed right open

let arr=[1,2,3,4]
let res=arr.slice(1)
let res2=arr.slice(1,2)
console.log(res)//[2,3,4]
console.log(res2)//[2]
console.log(arr)//[1,2,3,4]

8. Splice (add / delete location, quantity to be deleted, new items added to the array [optional]) is used to add / delete items to / from the array, and then return a new array containing deleted items (change the original array)

let arr=[1,2,3,4,5,6]
console.log(arr.splice(1,2))//[2,3]
console.log(arr)//[1,4,5,6]
let arr2=[6,7,8,9,10]
console.log(arr2.splice(1,2,0))//[7,8]
console.log(arr2)//[6,0,9,10]

9. map() returns a new array. The elements in the array are the values of the original array elements after calling the function (the original array will not be changed)

map() does not detect empty arrays

let arr=[1,2,3,4,5,6]
let res=arr.map(function(v,i){
			return v*2
		})
console.log(res)//[2,4,6,8,10,12]

10. includes() is used to determine whether an array contains a specified value. If yes, it returns true; otherwise, it returns false

let arr=[1,2,3,4,5,6]
let flag=arr.includes(4)
console.log(flag)//true
console.log(arr.includes(7))//false

11. flat() is used to "flatten" nested arrays into one-dimensional arrays. This method returns a new array (without changing the original array)

Parameter: Specifies the structure depth of the nested array to be extracted. The default value is 1.

console.log([1,[2]].flat())//Output: [1,2]
console.log([1,[2,[3]]].flat(2))//Output: [1,2,3]
console.log([1,[2,[3,[4]]]].flat(Infinity))//Output: [1,2,3,4]

12. every() is used to check whether all elements of the array meet the specified conditions. Only when all elements meet the specified conditions will it return true (the original array will not be changed)

Empty arrays are not detected

let arr=[1,2,3,4]
let res=arr.every(function(v,i){
	return v<5
})
console.log(res)//true

13. some() is used to detect whether the elements in the array meet the specified conditions. If one element meets the conditions, it returns true (without changing the original array)

Empty arrays are not detected

let arr=[1,2,3,4]
let res=arr.some(function(v,i){
	return v<3
})
console.log(res)//true

14. filter() returns a new array. The elements in the new array are checked by checking all qualified elements in the specified array (without changing the original array)

Empty arrays are not detected

let arr=[1,2,3,4]
let res=arr.filter(function(v,i){
	return v<3
})
console.log(res)//[1,2]

15. find() returns the value of the first element of the array that passed the test (judgment in the function), and returns the specific value (without changing the original array)

For empty arrays, the function does not execute

let arr=[1,2,3,4]
let res=arr.find(function(v,i){
	return v<3
})
console.log(res)//1

16. findIndex() returns the position of the first element of an array whose test conditions (functions) meet the conditions (without changing the original array)

let arr=[1,2,3,4]
let res=arr.findIndex(function(v,i){
	return v>3
})
console.log(res)//3

17. join() is used to convert all elements in the array into a string. The elements are separated by the specified separator

let arr=[1,2,3,4]
console.log(arr.join())//1,2,3,4
console.log(arr.join(''))//1234
console.log(arr.join('-'))//1-2-3-4

18. reverse() is used to reverse the array (change the original array)

let arr=[1,2,3,4]
console.log(arr.reverse())//[4,3,2,1]
console.log(arr)//[4,3,2,1]

19. sort() is used to sort the array elements (changing the original array)

The default sort order is ascending alphabetically

            let arr=[1,12,3,4,67]
			console.log(arr.sort())//[1,12,3,4,67]
			console.log(arr.sort(function(a,b){
				return a-b
			}))//[1,3,4,12,67]

20. forEach(function(v,i) {}) traverses the array (does not change the original array)

character string

1. indexOf(searchvalue,start) returns the first occurrence of a specified string value in the string

If no matching string is found, - 1 is returned

Parameter: start optional, indicating the location where retrieval starts

            let str='shgdjfj'
			console.log(str.indexOf('j'))//4
			console.log(str.indexOf('j',5))//6

2. lastIndexOf() searches the string back and forward, and calculates the last position of the returned string from the starting position (0)

            let str='shgdjfj'
			console.log(str.lastIndexOf('j'))//6
			console.log(str.lastIndexOf('j',5))//4

3. split() divides a string into an array of strings (without changing the original string)

            let str='aaabbb'
			console.log(str.split())//[aaabbb]
			console.log(str.split(""))//[a,a,a,b,b,b]

4. charAt() returns the character at the specified position (without changing the original string)

            let str='asj56b'
			console.log(str.charAt(3))//5

5. substr(start,num) extracts a specified number of characters from the string from the start index number

The num parameter is optional

            let str='asj56b'
			console.log(str.substr(1))//sj56b
			console.log(str.substr(1,3))//sj5

6. substring(start,end) extracts the characters between two specified index numbers in the string (closed on the left and open on the right)

            let str='asj56b'
			console.log(str.substring(1))//sj56b
			console.log(str.substring(1,4))//sj5

7. toLowerCase() converts the string to lowercase

8. toUpperCase() converts the string to uppercase

9. slice(start,end) extracts a fragment of a string and returns the extracted part (closed on the left and open on the right) in a new string

The end parameter is optional

            let str='asj56b'
			console.log(str.slice(1))//sj56b
			console.log(str.slice(1,2))//s

10. trim() removes the whitespace at the beginning and end of the character (does not change the original string)

The trim() method is not applicable to null, undefined, Number types

            let str=' I love you '
			console.log(str.trim())//I love you

11. includes() finds whether the string contains the specified substring. If yes, it returns true

            let str=' Iloveyou '
			console.log(str.includes('o'))//true

12. search() is used to retrieve a substring specified in a string or a substring that matches a regular expression

Returns the first occurrence of a substring

If no matching substring is found, - 1 is returned

            let str='Iloveyou'
			console.log(str.search('o'))//2

13. replace() is used to replace some characters in a string with others, or to replace a substring that matches a regular expression

            let str='Iloveyou'
			console.log(str.replace('o','M'))//IlMveyou
			console.log(str.replace(/o/g,'M'))//IlMveyuM

14. concat() concatenates one or more strings and returns a new string

            let str='shgdjfj'
			console.log(str.concat('sss'))//shgdjfjsss

15. match() retrieves the specified value within a string or finds a match for one or more regular expressions

            let str='Iloveyou'
			console.log(str.match('y'))//["y",index:5,input:"Iloveyou"]
			console.log(str.match(/o/g))//['o','o']

16. startWith() is used to detect whether the string starts with the specified substring. If yes, it returns true

let str = "Hello world"
let n = str.startsWith("Hello")//true

Posted by Grant Holmes on Fri, 24 Sep 2021 08:05:42 -0700