Commonly used methods for js arrays? (with examples); Common methods for strings

Table of contents

1. Array constant methods

1.Array.map()

2.Array.forEach()

3.Array.filter()

4.Array.every()

5.Array.some()

6.Array.reduce()

7.Array.push()

8.Array.pop()

9.Array.shift()

10.Array.unshift()

11.Array.isArray()

12.Array.concat()

​ 13.Array.toString() ​

15.Array.splice()

2. Common operations on strings


1. Array constant methods

1.Array.map()

This method calls a provided function on each element in the array, and returns the result as a new array without changing the original array.

let arr = [1, 2, 3, 4, 5];
let newArr = arr.map(x => x * 2);
//arr= [1, 2, 3, 4, 5]   原数组保持不变
//newArr = [2, 4, 6, 8, 10] 返回新数组

2.Array.forEach()

This method is to execute each element in the array and pass it into the provided function. There is no return value and the original array is directly changed. Note that it is different from the map method.

let arr = [1, 2, 3, 4, 5];
num.forEach(x => x * 2);
// arr = [2, 4, 6, 8, 10]  数组改变,注意和map区分

3.Array.filter()

This method judges all elements and returns the elements that meet the conditions as a new array.

let arr = [1, 2, 3, 4, 5]
const isBigEnough => value => value >= 3
let newArr = arr.filter(isBigEnough)
//newNum = [3, 4, 5] 满足条件的元素返回为一个新的数组

4.Array.every()

This method judges all elements and returns a Boolean value. If all elements meet the judgment conditions, it returns true, otherwise it returns false:

let arr = [1, 2, 3, 4, 5]
const isLessThan4 => value => value < 4
const isLessThan6 => value => value < 6
arr.every(isLessThan4) //false
arr.every(isLessThan6) //true

5.Array.some()

This method judges all elements and returns a Boolean value. If there are elements that meet the judgment conditions, it returns true. If all elements do not meet the judgment conditions, it returns false:

let arr = [1, 2, 3, 4, 5]
const isLessThan4 => value => value < 4
const isLessThan6 => value => value > 6
arr.some(isLessThan4) //true
arr.some(isLessThan6) //false

6.Array.reduce()

This method calls the return function on all elements, and the return value is the final result. The value passed in must be of function type:

let arr = [1, 2, 3, 4, 5];
const add = (a, b) => a + b;
let sum = arr.reduce(add);
//sum = 15  相当于累加的效果

Correspondingly, there is also an Array.reduceRight() method. The difference is that this one operates from right to left.

7.Array.push()

This method adds new elements at the end of the array. This method changes the length of the array:

8.Array.pop()

This method removes the last element after the array and returns the array. This method changes the length of the array:

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

9.Array.shift()

This method removes the first element after the array and returns the array. This method changes the length of the array:

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

10.Array.unshift()

This method adds one or more elements to the beginning of the array and returns the length of the new array:

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

11.Array.isArray()

Determine whether an object is an array and return a Boolean value

12.Array.concat()

This method is a way to concatenate multiple arrays into one array:

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

​ 13.Array.toString() ​

This method converts an array into a string:

 let arr = [1, 2, 3, 4, 5]; 
let str = arr.toString() 
console.log(str) // 1,2,3,4,5 

​ 14.Array.join() ​  

This method also converts the array into a string:

 let arr = [1, 2, 3, 4, 5];

let str1 = arr.toString() l

et str2 = arr.toString(',')

let str3 = arr.toString('##')

console.log(str1) // 12345

console.log(str2) // 1,2,3,4,5

console.log(str3) // 1##2##3##4##5​`

You can see the difference from toString through examples. You can set the interval between elements.

15.Array.splice()

Starting position, number of deleted items, elements)

A universal method that can add, delete, and modify:

let arr = [1, 2, 3, 4, 5];
let arr1 = arr.splice(2, 0 'haha')
let arr2 = arr.splice(2, 3)
let arr1 = arr.splice(2, 1 'haha')
console.log(arr1) //[1, 2, 'haha', 3, 4, 5]新增一个元素
console.log(arr2) //[1, 2] 删除三个元素
console.log(arr3) //[1, 2, 'haha', 4, 5] 替换一个元素

2. Common operations on strings

  • charAt(index): Returns the string at the specified index

  • charCodeAt(index): Returns the Unicode value of the character at the specified index

  • concat(str1, str2, ...): Concatenate multiple strings and return a copy of the concatenated strings

  • fromCharCode(): Converts Unicode values ​​into actual strings

  • indexOf(str): Returns the position of the first occurrence of str in the parent string, if not, returns -1

  • lastIndexOf(str): Returns the position of the last occurrence of str in the parent string, if not, returns -1

  • match(regex): Search a string and return all matches of the regular expression

  • replace(str1, str2): str1 can also be a regular expression, replace str1 with str2

  • search(regex): Search a string based on a regular expression and return the first matching position

  • slice(start, end): Returns the substring with character index between start and end (exclusive)

  • split(sep, limit): Split the string into a character array, limit is the maximum number of splits to be performed from the beginning

  • substr(start, length): Starting from the position of character index start, return a substring of length length

  • substring(from, to): Returns the substring whose character index is between from and to (exclusive)

  • toLowerCase(): Convert string to lowercase

  • toUpperCase(): Convert string to uppercase

  • valueOf(): Returns the original string value

Guess you like

Origin blog.csdn.net/smallmww/article/details/132432764