1. String

1.1 Gets the corresponding subscript value of the string

const str = 'abc123ABC'
console.log(str[1])  // The output is: b
console.log(str.chatAt(1))  // The output is: b
Copy the code

1.2 Obtaining the length of the String

const str = "123456"
console.log(str.length)  // The length is 6
Copy the code

1.3 Get the Unicode encoding of the string

const str = 'China'
console.log(str.charCodeAt(0))  // The Unicode encoding is 20013
Copy the code

If no subscript is specified, 0 is obtained by default

1.4 Unicode encoding to string

const arr = [25105.29233.20320]
console.log(String.fromCharCode(arr[0], arr[1], arr[2]))  // The output is: I love you
Copy the code

1.5 Checking whether the corresponding character string exists

const str = 'layouwen = layouwen'
console.log(str.indexOf('l'))  // find return subscript 0
console.log(str.indexOf('1'.4)  // start at subscript 4 and return subscript 11
console.log(str.indexOf('s'))  // Return -1 if not found
Copy the code

1.6 Search for the last index from back to front

const str = 'hello word'
console.log(str.lastIndexOf('o'))  / / 7
console.log(str.lastIndexOf('o'.8))  / / 7
console.log(str.lastIndexOf('o'.5))  / / 4
Copy the code

1.7 Intercepting A Character String

const str = '12345678'
console.log(str.slice(0, -1))  // Start at subscript 0 and end before the penultimate string
/ / 1234567

console.log(str.subString(1, str.length - 1))  / / 234567
Copy the code

1.8 Split strings into arrays

const str = 'la-you-wen'
console.log(str.split())  // ['la-you-wen']
console.log(str.split(The '-'))  // ['la', 'you', 'wen']
console.log(str.split(The '-'.1) // split only one ['la']
Copy the code

1.9 Connection String

const str1 = 'la'
const str2 = 'you'
const str3 = 'wen'
console.log(str1.concat(str2, str3))  // layouwen
Copy the code

1.10 Case Conversion

const str1 = 'abc'
const str2 = 'DEF'
console.log(str1.toUpperCase())  // ABC
console.log(str2.toLowerCase())  // def
Copy the code

1.11 Remove the first space

const str = '123'
console.log(str.trim())  / / 123 4
Copy the code

2, arrays,

2.1 Adding at the end

const arr = [1.2.3]
console.log(arr.push(4.5))  // Return the length of the added array: 5
console.log(arr)  // [1, 2, 3, 4, 5]
Copy the code

2.2 End Deletion

const arr = [4.6.2]
console.log(arr.pop())  // Returns the deleted value: 2
console.log(arr)  / / [4, 6]
Copy the code

2.3 Adding at the beginning

const arr = [4.2.3]
console.log(arr.unshift(1))  // Return the array length: 4
console.log(arr)  // [1, 4, 2, 3]
Copy the code

2.4 Initial Deletion

const arr = [7.5.2]
console.log(arr.shift())  // Returns the deleted value: 7
console.log(arr)  / / (5, 2)
Copy the code

2.5 Add, Delete, and Replace

const arr1 = [1.2.3]
arr1.splice(1.0.4.5.6)
console.log(arr1)  // [1, 4, 5, 6, 2, 3]

const arr2 = [1.2.3]
console.log(arr2.splice(1.2))  [2, 3] [2, 3]
console.log(arr2)  / / [1]

const arr3 = [1.2.3.4]
console.log(arr3.splice(1.3.'a'.'b'.'c'))  [2, 3, 4] // Add a, b, and c to the index.
console.log(arr3)  // [1, 'a', 'b', 'c']
Copy the code

2.6 the sorting

const arr = [1.2.6.3.5.9.0]
arr.sort((a, b) = >a-b)  B - a is in descending order
console.log(arr)
Copy the code

2.7 Array Stitching

const arr1 = [1.2.3]
const arr2 = [4.5.6]
const arr3 = ['a'.'b']
console.log(arr1.concat(arr2, arr3))  // Return the concatenated array
// [1, 2, 3, 4, 5, 6, "a", "b"]
console.log(arr1)  //  [1, 2, 3]
Copy the code

2.8 Array to string

const arr = ['hello'.'word'.'hello']
console.log(arr.join(', '))  // Returns the concatenated string
/ / hello, word, how are you
Copy the code

2.9 Array Inversion

const arr = [1.2.3.7.6.5.4.0]
console.log(arr.reverse())  // Return the inverted array
// [0, 4, 5, 6, 7, 3, 2, 1]
console.log(arr)  // The original array is also changed [0, 4, 5, 6, 7, 3, 2, 1]
Copy the code

2.10 There are values in array lookup from front to back

const arr = ['a'.'b'.'c'.'a'.'d']
console.log(arr.indexOf('a'.3))  // start with subscript 3
/ / 3
console.log(arr.indexOf('a', -1))  // start from the bottom of the list
// -1
Copy the code

2.11 There is a value in array lookup from back to front

const arr = ['a'.'b'.'c'.'a'.'d']
console.log(arr.lastIndexOf('a'.2))  // look left from subscript 2
/ / 0
console.log(arr.lastIndexOf('a', -1))  // Start from the last one to the left
/ / 3
Copy the code

2.12 Array Cutting

const arr = ['a'.'b'.'c'.'a'.'d']
console.log(arr.slice(1.4))  // ['b', 'c', 'a']
console.log(arr.slice(-3.1))  / / []
console.log(arr.slice(-3, -1))  // ['c', 'a']
console.log(arr)  // ['a', 'b', 'c', 'a', 'd']
Copy the code

2.13 Go through the number group

const demoArr = ['a'.'b'.'c'.'a'.'d']
const thisArr = ['I'm changing this'.'Array of']
demoArr.forEach(function(value, index, arr){
  console.log(value, index, arr)
  console.log(this)
},thisArr)  // Change the this pointer in the callback
demoArr.forEach((value, index, arr) = >{  // The arrow function has no this, so the second argument is invalid
  console.log(value, index, arr)
})
Copy the code

2.14 Array Filtering

const demoArr = ['a'.'b'.'c'.'a'.'d']
const thisArr = ['I'm changing this'.'Array of']
const newArr1 = demoArr.filter(function (value, index, arr) {
  console.log(value, index, arr)
  console.log(this)
  returnvalue ! = ='c' // Return all true results
}, thisArr) // Change the this pointer in the callback
console.log('I am a new array:')
console.log(newArr1)  // ["a", "b", "a", "d"]
console.log('I am the original array:')
console.log(demoArr)  // ["a", "b", "c", "a", "d"]
const newArr2 = demoArr.filter((value, index, arr) = > {
  // The arrow function has no this, so the second argument is invalid
  console.log(value, index, arr)
  return value === 'c'
})
console.log(newArr2)  // ['c']
Copy the code

2.15 Array processing

const demoArr = ['a'.'b'.'c'.'a'.'d']
const thisArr = ['I'm changing this'.'Array of']
const newArr = demoArr.map((item, index, arr) = > {
  console.log(item, index, arr)
  return (item += 'well? ') // Add the result to the new array and return it
})
console.log(newArr) // [" A huh? ", "b huh? ", "C huh? "," A huh? ", "D huh? "]
console.log(demoArr) // ["a", "b", "c", "a", "d"]
demoArr.map(function (item, index, arr) {
  console.log(this) // [' I am changing this', 'array ']
}, thisArr)
Copy the code

2.16 Data set processing

const demoArr = [10.10.10.10]
const result = demoArr.reduce((sum, value, index) = > { // sum is the last result
  return sum + value
}, 1000) // The initial value is the first digit if not passed
console.log(result) / / 1040
Copy the code

2.17 If one of them is true, true is returned

const falsyArr1 = [false.1.0.0]
const falsyArr2 = [false.undefined.0.0]
const status1 = falsyArr1.some((value, index, arr) = > {
  console.log(value, index, arr)
  return value // Return true if one of them is true, false otherwise
})
const status2 = falsyArr2.some(value= > value)
console.log(status1, status2) // true false
Copy the code

2.18 Return true only if all items are true

const falsyArr1 = [1.1.true.0]
const falsyArr2 = ['a'.true.1.1]
const status1 = falsyArr1.every((value, index, arr) = > {
  console.log(value, index, arr)
  return value // Return true if all values are true, false otherwise
})
const status2 = falsyArr2.every(value= > value)
console.log(status1, status2) // false true
Copy the code

3, objects,

3.1 Obtaining All Keys

const obj = {
  name: 'layouwen'.age: '21'
}
console.log(Object.keys(obj)) // ["name", "age"]
Copy the code

3.2 Obtaining All Values

const obj = {
  name: 'layouwen'.age: '21'
}
console.log(Object.values(obj)) // ["layouwen", "21"]
Copy the code

3.3 Deleting a Key/Value Pair

const obj = {
  name: 'layouwen'.age: '21'
}
delete obj.age // Delete key-value pairs
console.log(obj) // {name: "layouwen"}
Copy the code

4, JSON

4.1 turn JSON

const obj = {
  name: 'layouwen'.age: 21
}
console.log(JSON.stringify(obj)) // {"name":"layouwen","age":21}
Copy the code

4.2 turn instance

const objJSON = '{"name": "yqq", "age": 30}'
console.log(JSON.parse(objJSON)) // {name: "yqq", age: 30}
Copy the code

The key in the string must be in double quotes

5, Math

5.1 PI

console.log(Math.PI) / / 3.141592653589793
Copy the code

5.2 take the whole

5.2.1 Rounded Up

console.log(Math.ceil(1.3)) / / 2
Copy the code

5.2.2 Round Down

console.log(Math.floor(1.3)) / / 1
Copy the code

5.2.3 Rounding

console.log(Math.round(1.4)) / / 1
console.log(Math.round(1.5)) / / 2
Copy the code

5.3 random number

console.log(Math.random()) // A random number between 0 and 1
Copy the code

A maximum of 5.4

console.log(Math.max(5, -1.9.4)) / / 9
const arr = [4.3.2.8.5]
console.log(Math.max(... arr))/ / 8
Copy the code

5.5 the minimum

console.log(Math.min(5, -1.9.4)) // -1
const arr = [4.3.2.8.5]
console.log(Math.min(... arr))/ / 2
Copy the code

5.6 absolute value

console.log(Math.abs(-10))
/ / 10
Copy the code