当前位置:网站首页>js的数组拼接「建议收藏」
js的数组拼接「建议收藏」
2022-06-27 09:33:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
js的数组拼接方法
方法一:concat方法拼接(返回一个新数组)
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
var newa = a1.concat(a2);
结果类型:object
concat方法:用于连接两个或多个数组,生成新数组,concat后面的数组时操作的时候数组的元素,而不是数组
方法二:join方法拼接(返回一个新数组)
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
var newa = a1.join()+’,’+a2.join();
返回结果:1,2,3,4,5,a,b,c
结果类型:string
join方法:把数组的所有元素放入一个字符串,然后把这些字符串连接起来,在两个元素之间插入 separator 字符串而生成的。separator 在join()不添加分割符时是逗号(‘,’),在join(‘.’)添加分割符时用提供的符号分割。
方法三:结合apply或者call使用
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
a1.push.apply(a1,a2);
结果类型:object
apply方法:编写用于不同对象的方法,将a1的push方法用在a1上,接收a2(数组类型)参数
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
a1.push.call(a1,…a2);
结果类型:object
call方法:调用一个对象的一个方法,以另一个对象替换当前对象,对象的参数为参数序列
说明:
call([thisObj[,arg1[, arg2[, [,.argN]]]]])
参数
1)thisObj 可选项。将被用作当前对象的对象。
2)arg1, arg2, , argN 可选项。将被传递方法参数序列。
方法四:es6的扩展运算符
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
a1.push(…a2)
结果类型:object
扩展运算符:好比rest参数的逆运算,将一个数组转化为逗号分割的参数序列,主要用于函数调用。
方法五:遍历数组
var a1 = [1,2,3,4,5];
var a2 = [‘a’,’b’,’c’];
a2.forEach((item) => {a1.push(item); })
结果类型:object
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/133635.html原文链接:https://javaforall.cn
边栏推荐
- I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
- i=i++;
- win10为任意文件添加右键菜单
- 不会初始化类的几种Case
- 使用Aspose.cells将Excel转成PDF
- E+h secondary meter repair pH transmitter secondary display repair cpm253-mr0005
- Unity - - newtonsoft. Analyse json
- HiTek电源维修X光机高压发生器维修XR150-603-02
- 有关二叉树的一些练习题
- 使用aspose-slides将ppt转pdf
猜你喜欢

视频文件太大?使用FFmpeg来无损压缩它

Privacy computing fat offline prediction

Rockermq message sending and consumption mode
Shortcut key bug, reproducible (it seems that bug is the required function [funny.Gif])

Apache POI的读写

Digital ic-1.9 understands the coding routine of state machine in communication protocol
![文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument](/img/08/2d4f425e6941af35616911672b6fed.png)
文件名设置导致writelines写入报错:OSError: [Errno 22] Invalid argument

C # solve the relative path problem using SQLite

E+H二次表维修PH变送器二次显示仪修理CPM253-MR0005

How do I get the STW (pause) time of a GC (garbage collector)?
随机推荐
Obsidian 一周使用心得(配置、主题和插件)
The most direct manifestation of memory leak
Brief introduction to SSL encryption process
std::memory_order_seq_cst内存序
DV scroll board width of datav rotation table component
Design of multiple classes
Five page Jump methods for wechat applet learning
Semi-supervised Learning入门学习——Π-Model、Temporal Ensembling、Mean Teacher简介
unity--newtonsoft. JSON parsing
Several cases that do not initialize classes
The background prompt module for accessing fastadmin after installation does not exist
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
[system design] proximity service
Summary of three basic interview questions
js 所有的网络请求方式
提高效率 Or 增加成本,开发人员应如何理解结对编程?
Analysis of key technologies for live broadcast pain points -- second opening, clarity and fluency of the first frame
js中的数组对象
Rockermq message sending and consumption mode
[vivid understanding] the meanings of various evaluation indicators commonly used in deep learning TP, FP, TN, FN, IOU and accuracy