博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在JavaScript中连接两个数组
阅读量:2504 次
发布时间:2019-05-11

本文共 853 字,大约阅读时间需要 2 分钟。

Suppose you have two arrays:

假设您有两个数组:

const first = ['one', 'two']const second = ['three', 'four']

and you want to merge them into one single array

您想将它们合并为一个数组

How can you do so?

你该怎么做?

The modern way is to use the destructuring operator, to create a brand new array:

现代方法是使用解构运算符来创建全新的数组:

const result = [...first, ...second]

This is what I recommend. Note that this operator was introduced in ES6, so older browsers (read: Internet Explorer) might not support it.

这就是我的建议。 请注意,此运算符是ES6中引入的,因此较旧的浏览器(阅读:Internet Explorer)可能不支持它。

If you want a solution that works also with older browsers, you could use the concat() method which can be called on any array:

如果您想要一个适用于较旧浏览器的解决方案,则可以使用concat()方法,该方法可以在任何数组上调用:

const result = first.concat(second)

Both methods will generate a new array, without modifying the existing ones.

两种方法都将生成一个新数组,而不修改现有数组。

翻译自:

转载地址:http://hlqgb.baihongyu.com/

你可能感兴趣的文章
在eclipse上用tomcat部署项目404解决方案
查看>>
web.xml 配置中classpath: 与classpath*:的区别
查看>>
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
2020-11-18
查看>>