当前位置:首页 > 科技  > 软件

六个你必须知道的 ES6 中很酷的数组函数

来源: 责编: 时间:2024-01-08 17:09:53 331观看
导读1. Array.of关于数组函数,众所周知,我们可以通过Array函数做以下事情。初始化指定长度的数组设置数组的初始值// 1. Initialize an array of the specified lengthconst array1 = Array(3) // [ , , ]// 2. Set the ini

1. Array.of

RK328资讯网——每日最新资讯28at.com

关于数组函数,众所周知,我们可以通过Array函数做以下事情。RK328资讯网——每日最新资讯28at.com

  • 初始化指定长度的数组
  • 设置数组的初始值
// 1. Initialize an array of the specified lengthconst array1 = Array(3) // [ , , ]// 2. Set the initial value of the arrayconst array2 = Array() // []const array3 = Array(undefined) // [ undefined ]const array4 = Array(1, 2, 3) // [ 1, 2, 3 ]

传递给Array函数的参数个数不一样,其功能也不一样。这常常让我感到困惑。RK328资讯网——每日最新资讯28at.com

幸运的是,我们可以使用Array.of来弥补Array的缺点。RK328资讯网——每日最新资讯28at.com

// it's not initializing an array of length 3const array1 = Array.of(3) // [ 3 ]const array2 = Array.of() // []const array3 = Array.of(undefined) // [ undefined ]const array4 = Array.of(1, 2, 3) // [ 1, 2, 3 ]

2. Array.from

在该方法中,我们可以通过 Array.from 方法将类数组对象、arguments 对象和 NodeList 对象转换为真正的数组。RK328资讯网——每日最新资讯28at.com

类似数组的对象RK328资讯网——每日最新资讯28at.com

const arrayLike = {  0: 'fatfish',  1: 'medium',  length: 2}const array1 = [].slice.call(arrayLike) // ['fatfish', 'medium']// A more convenient wayconst array2 = Array.from(arrayLike) // ['fatfish', 'medium']

1). 节点列表

const domsNodeList = document.querySelectorAll('div')const domsArray = Array.from(domsNodeList) // [ dom, dom, dom, ... ]

2). Arguments

const logInfo = function () {  console.log('arguments', arguments)  console.log('Array.from arguments', Array.from(arguments))}logInfo('fatfish', 100)logInfo('fatfish')

3).Array.from的第二个参数

我们可以使用 Array.from 方法,例如“[].map”。RK328资讯网——每日最新资讯28at.com

const array = [ 1, 2, 3 ]const array2 = array.map((num) => num * 2) // [2, 4, 6]const array3 = Array.from(array, (num) => num * 2) // [2, 4, 6]

3. includes

我们经常会写这样的判断语句,当其中一个条件满足时做某事。RK328资讯网——每日最新资讯28at.com

const num = 1if (num === 1 || num === 2 || num === 3 || num === 4) {  console.log(num) // 1}

其实可以通过include方法来简化代码。RK328资讯网——每日最新资讯28at.com

const nums = [ 1, 2, 3, 4 ]const num = 1if (nums.includes(num)) {  console.log(num) // 1}

4.使用at方法读取数组尾部元素

如何读取数组的尾部元素?是的,我们需要以“array.length-1”作为下标来读取。RK328资讯网——每日最新资讯28at.com

const array = [ 1, 2, 3, 4, 5 ]const lastEle = array[ array.length - 1 ] // 5// You can't read like thatconst lastEle = array[ - 1 ] // undefined

还有其他办法吗?RK328资讯网——每日最新资讯28at.com

是的,“at”方法将是你的魔力。当然,您可以读取数组中其他位置的元素。RK328资讯网——每日最新资讯28at.com

const array = [ 1, 2, 3, 4, 5 ]const lastEle = array.at(-1) // 5const ele1 = array.at(0) // 1

5. flat

来自 MDN:“flat() 方法创建一个新数组,其中所有子数组元素递归地连接到其中,直到指定的深度。”RK328资讯网——每日最新资讯28at.com

const array = [ 1, [ 2, [ 3, [ 4, [ 5 ] ] ] ] ]// The default depth is 1const flat1 = array.flat() // [ 1, 2, [ 3, [ 4, [ 5 ] ] ] ]const flat2 = array.flat(2) // [ 1, 2, 3, [ 4, [ 5 ] ] ]const flatAll = array.flat(Infinity) // [ 1, 2, 3, 4, 5 ]

6.findIndex

来自 MDN:“findIndex() 方法返回数组中满足所提供的测试函数的第一个元素的索引。否则,返回-1,表示没有元素通过测试。”RK328资讯网——每日最新资讯28at.com

const array = [ -1, 0, 10, 10,  20, 100 ]const index1 = array.findIndex((num) => num < 0) // 0const index2 = array.findIndex((num) => num >= 10) // 2


RK328资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-58895-0.html六个你必须知道的 ES6 中很酷的数组函数

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 12 个动态 JavaScript 动画库增强用户体验

下一篇: 如何自己实现一个静态代码分析工具?

标签:
  • 热门焦点
  • 小米官宣:2023年上半年出货量中国第一!

    今日早间,小米电视官方微博带来消息,称2023年小米电视上半年出货量达到了中国第一,同时还表示小米电视的巨屏风暴即将开始。“公布一个好消息2023年#小米电视上半年出货量中国
  • 7月安卓手机好评榜:三星S23Ultra好评率第一

    性能榜和性价比榜之后,我们来看最后的安卓手机好评榜,数据来源安兔兔评测,收集时间2023年7月1日至7月31日,仅限国内市场。第一名:三星Galaxy S23 Ultra好评率:95.71%在即将迎来新
  • 28个SpringBoot项目中常用注解,日常开发、求职面试不再懵圈

    前言在使用SpringBoot开发中或者在求职面试中都会使用到很多注解或者问到注解相关的知识。本文主要对一些常用的注解进行了总结,同时也会举出具体例子,供大家学习和参考。注解
  • 摸鱼心法第一章——和配置文件说拜拜

    为了能摸鱼我们团队做了容器化,但是带来的问题是服务配置文件很麻烦,然后大家在群里进行了“亲切友好”的沟通图片图片图片图片对比就对比,简单对比下独立配置中心和k8s作为配
  • CSS单标签实现转转logo

    转转品牌升级后更新了全新的Logo,今天我们用纯CSS来实现转转的新Logo,为了有一定的挑战性,这里我们只使用一个标签实现,将最大化的使用CSS能力完成Logo的绘制与动画效果。新logo
  • Golang 中的 io 包详解:组合接口

    io.ReadWriter// ReadWriter is the interface that groups the basic Read and Write methods.type ReadWriter interface { Reader Writer}是对Reader和Writer接口的组合,
  • 从 Pulsar Client 的原理到它的监控面板

    背景前段时间业务团队偶尔会碰到一些 Pulsar 使用的问题,比如消息阻塞不消费了、生产者消息发送缓慢等各种问题。虽然我们有个监控页面可以根据 topic 维度查看他的发送状态,
  • 超级标准版旗舰!iQOO 11S全球首发iQOO超算独显芯片

    上半年已接近尾声,截至目前各大品牌旗下的顶级旗舰都已悉数亮相,而下半年即将推出的顶级旗舰已经成为了数码圈爆料的主流,其中就包括全新的iQOO 11S系
  • 亲历马斯克血洗Twitter,硅谷的苦日子在后头

    文/刘哲铭  编辑/李薇  马斯克再次挥下裁员大刀。  美国时间11月14日,Twitter约4400名外包员工遭解雇,此次被解雇的员工的主要工作为内容审核等。此前,T
Top