当前位置:网站首页>JS中复制数组
JS中复制数组
2022-04-23 07:34:00 【beinlife】
JS中复制数组
var array1 = new Array("1","2","3");
var array2;
array2 = array1;
array1.length = 0;
alert(array2); //返回为空
这种做法是错的,因为javascript分原始类型与引用类型(与java、c#类似)。Array是引用类
型。array2得到的是引用,所以对array1的修改会影响到array2。
可使用slice()进行复制,因为slice()返回也是数组。
var array1 = new Array("1","2","3");
var array2;
array2 = array1.slice(0);
array1.length = 0;
alert(array2); //返回1、2、3
注意concat()返回的并不是调用函数的Array,而是一个新的Array,所以可以利用这一点进行复制。
var array1 = new Array("1","2","3");
var array2;
array2 = array2.concat(array1);
array1.length = 0;
alert(array2); //返回1、2、3
http://blog.sina.com.cn/s/blog_51baa1570100io8r.html
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/52987426
边栏推荐
- The third divisor of leetcode simple question
- ASAN 极简原理
- Ansible Automation Operation and Maintenance details (ⅰ) Installation and Deployment, Parameter use, list Management, Profile Parameters and user level ansible operating environment Construction
- Compiler des questions de principe - avec des réponses
- 谈谈那些基础但不简单的股票数据
- Qt读取路径下所有文件或指定类型文件(含递归、判断是否为空、创建路径)
- js将树形结构数据转为一维数组数据
- nn.Module类的讲解
- Briefly describe the hierarchical strategy of memory
- Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
猜你喜欢

ATSS(CVPR2020)

LeetCode简单题之三除数

WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code

An article understands variable lifting

欧圣电气深交所上市:市值52亿 陆为东父女为美国籍

Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结

Campus transfer second-hand market source code download

Flink SQL实现流批一体

Rotation function of leetcode medium problem

dried food! Point based: differentiable Poisson solver
随机推荐
Samsung, March to the west again
An example of network communication based on TCP / IP protocol -- file transmission
LeetCode15. Sum of three
Distributed service governance Nacos
LeetCode中等题之旋转函数
一个必看的微信小程序开发指南1-基础知识了解
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
获取TrustedInstaller权限
利用Js实现一个千分位
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
【路科V0】验证环境2——验证环境组件
Multi vision slam
synchronized 实现原理
Data deletion and modification (MySQL)
dried food! Point based: differentiable Poisson solver
数论求a^b(a,b为1e12级别)的因子之和
为什么会存在1px问题?怎么解决?
Usage of databinding
freertos学习02-队列 stream buffer message buffer
[learning] audio and video development from scratch (9) -- nuplayer