收集的JS使用技巧

JavaScript常用方法

数组是 JS 最常见的一种数据结构,咱们在开发中也经常用到,在这篇文章中,提供一些小技巧,帮助咱们提高开发效率。

删除数组中的重复项

const fruits = [
  'banana',
  'apple',
  'orange',
  'watermelon',
  'banana',
  'apple',
  'orange'
]

// 第一种方式
const uniqueFruits = Array.from(new Set(fruits))
console.log(uniqueFruits) // [ 'banana', 'apple', 'orange', 'watermelon' ]

// 第二种方式
const uniqueFruits2 = [...new Set(fruits)]
console.log(uniqueFruits2) // [ 'banana', 'apple', 'orange', 'watermelon' ]

替换数组中的特定值

Array.form 达到 .map 的效果

置空数组

将数组转换为对象

将数据填充数组

数组合并

求两个数组的交集

从数组中删除虚值

在 JS 中,虚值有 false, 0,’’, null, NaN, undefined。咱们可以 .filter() 方法来过滤这些虚值。

从数组中获取随机值

反转数组

lastIndexOf() 方法

对数组中的所有值求和

邮箱验证

最后更新于