基本类型

  • undefined
  • null
  • boolean
  • number
  • string
  • symbol
  • bigint

引用类型

  • array

  • object

  • function

  • regexp

  • date

内置的对象

  • String(常用)

  • Boolean

  • Number

  • Array(常用)

  • Object

  • Function

  • Math(常用)

  • Date(常用)

  • RegExp

数组

  • push()

  • pop()

  • sort()

  • splice()

  • join()

  • map()

  • filter()

  • reduce()

  • concat()

  • reverse()

类型判断

  • typeof():判断基本类型

  • instanceof():判断引用类型

清除数组中的重复元素

  • 使用Set

1
2
3
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = [...new Set(array)]
console.log(uniqueArray) // [1, 2, 3, 4, 5]
1
2
3
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = Array.from(new Set(array))
console.log(uniqueArray) // [1, 2, 3, 4, 5]
  • 使用filterindexOf

1
2
3
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = array.filter((value, index, self) => self.indexOf(value) === index)
console.log(uniqueArray) // [1, 2, 3, 4, 5]
  • 使用reduce

1
2
3
4
5
6
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = array.reduce((acc, value) => {
if (!acc.includes(value)) acc.push(value)
return acc
}, [])
console.log(uniqueArray) // [1, 2, 3, 4, 5]
  • Lodash库

1
2
3
4
import _ from "lodash"
const array = [1, 2, 2, 3, 4, 4, 5]
const uniqueArray = _.uniq(array)
console.log(uniqueArray) // [1, 2, 3, 4, 5]