JavaScript的一些小技巧分享

作者:小蘑菇 时间:2024-04-23 09:29:01 

数组去重

ES6提供了几种简洁的数组去重的方法,但该方法并不适合处理非基本类型的数组。对于基本类型的数组去重,可以使用... new Set()来过滤掉数组中重复的值,创建一个只有唯一值的新数组。


const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)];
console.log(uniqueArray);

> Result:(4) [1, 2, 3, 5]

这是ES6中的新特性,在ES6之前,要实现同样的效果,我们需要使用更多的代码。该技巧适用于包含基本类型的数组:undefined、null、boolean、string和number。如果数组中包含了一个object,function或其他数组,那就需要使用另一种方法。

除了上面的方法之外,还可以使用Array.from(new Set())来实现:


const array = [1, 1, 2, 3, 5, 5, 1]
Array.from(new Set(array))

> Result:(4) [1, 2, 3, 5]

另外,还可以使用Array的.filter及indexOf()来实现:


const array = [1, 1, 2, 3, 5, 5, 1]
array.filter((arr, index) => array.indexOf(arr) === index)

> Result:(4) [1, 2, 3, 5]

注意,indexOf()方法将返回数组中第一个出现的数组项。这就是为什么我们可以在每次迭代中将indexOf()方法返回的索引与当索索引进行比较,以确定当前项是否重复。

确保数组的长度

在处理网格结构时,如果原始数据每行的长度不相等,就需要重新创建该数据。为了确保每行的数据长度相等,可以使用Array.fill来处理


let array = Array(5).fill('');
console.log(array);
> Result: (5) ["", "", "", "", ""]

数组映射

不使用Array.map来映射数组值的方法。


const array = [
{
name: '大漠',
email: 'w3cplus@hotmail.com'
},
{
name: 'Airen',
email: 'airen@gmail.com'
}]
const name = Array.from(array, ({ name }) => name)

> Result: (2) ["大漠", "Airen"]

数组截断

如果你想从数组末尾删除值(删除数组中的最后一项),有比使用splice()更快的替代方法。

例如,你知道原始数组的大小,可以重新定义数组的length属性的值,就可以实现从数组末尾删除值:


let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(array.length)
> Result: 10

array.length = 4
console.log(array)
> Result: (4) [0, 1, 2, 3]

这是一个特别简洁的解决方案。但是,slice()方法运行更快,性能更好:


let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array = array.slice(0, 4);
console.log(array);

> Result: [0, 1, 2, 3]

过滤掉数组中的falsy值

如果你想过滤数组中的falsy值,比如0、undefined、null、false,那么可以通过map和filter方法实现:


const array = [0, 1, '0', '1', '大漠', 'w3cplus.com', undefined, true, false, null, 'undefined', 'null', NaN, 'NaN', '1' + 0]
array.map(item => {
return item
}).filter(Boolean)

> Result: (10) [1, "0", "1", "大漠", "w3cplus.com", true, "undefined", "null", "NaN", "10"]

获取数组的最后一项

数组的slice()取值为正值时,从数组的开始处截取数组的项,如果取值为负整数时,可以从数组末属开始获取数组项。


let array = [1, 2, 3, 4, 5, 6, 7]
const firstArrayVal = array.slice(0, 1)
> Result: [1]

const lastArrayVal = array.slice(-1)
> Result: [7]

console.log(array.slice(1))
> Result: (6) [2, 3, 4, 5, 6, 7]

console.log(array.slice(array.length))
> Result: []

正如上面示例所示,使用array.slice(-1)获取数组的最后一项,除此之外还可以使用下面的方式来获取数组的最后一项:


console.log(array.slice(array.length - 1))
> Result: [7]

从数组中获取最大值和最小值

可以使用Math.max和Math.min取出数组中的最大小值和最小值:


const numbers = [15, 80, -9, 90, -99]
const maxInNumbers = Math.max.apply(Math, numbers)
const minInNumbers = Math.min.apply(Math, numbers)

console.log(maxInNumbers)
> Result: 90

console.log(minInNumbers)
> Result: -99

另外还可以使用ES6的...运算符来完成:


const numbers = [1, 2, 3, 4];
Math.max(...numbers)
> Result: 4

Math.min(...numbers)
> Result: 1

来源:https://segmentfault.com/a/1190000038716777

标签:JavaScript,技巧
0
投稿

猜你喜欢

  • eWebEditor不支持IE8的解决方法

    2009-11-02 10:59:00
  • Python自然语言处理之词干,词形与最大匹配算法代码详解

    2023-07-23 04:48:37
  • 详解pytorch的多GPU训练的两种方式

    2023-08-04 09:58:29
  • JS实现水平遍历和嵌套递归操作示例

    2024-04-22 12:48:01
  • asp如何创建目录?

    2009-11-14 20:51:00
  • python中类和实例如何绑定属性与方法示例详解

    2022-01-02 07:22:12
  • python 使用pandas计算累积求和的方法

    2021-05-22 19:46:16
  • Tensorflow全局设置可见GPU编号操作

    2021-04-21 12:41:46
  • 小 200 行 Python 代码制作一个换脸程序

    2021-02-12 09:47:52
  • 用JavaScript实现UrlEncode和UrlDecode功能

    2008-01-27 11:30:00
  • 解决Pytorch dataloader时报错每个tensor维度不一样的问题

    2022-10-06 20:08:56
  • mysql 获取规定时间段内的统计数据

    2024-01-24 11:25:10
  • Django 构建模板form表单的两种方法

    2021-07-01 23:44:41
  • 如何快速通过XSL转换XML文件

    2023-07-02 21:22:40
  • Python的组合模式与责任链模式编程示例

    2023-05-08 07:03:43
  • vue3+ts如何通过lodash实现防抖节流详解

    2024-05-02 16:32:13
  • PyTorch中permute的基本用法示例

    2022-04-22 01:14:01
  • SQLServer 2008中通过DBCC OPENTRAN和会话查询事务

    2024-01-13 15:35:55
  • 浏览器是怎样工作的二:渲染引擎 HTML解析

    2012-05-09 20:34:20
  • 提高JavaScript执行效率的23个实用技巧

    2023-08-15 18:38:12
  • asp之家 网络编程 m.aspxhome.com