当前位置:网站首页>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
边栏推荐
- 万物互联下如何对设备进行加密
- freertos学习02-队列 stream buffer message buffer
- Briefly describe the hierarchical strategy of memory
- clang 如何产生汇编文件
- vslam PPT
- Talk about the basic but not simple stock data
- 编译原理题-带答案
- js常用数组方法
- 跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
- C outputs a two-dimensional array with the following characteristics.
猜你喜欢

【学习】从零开始的音视频开发(9)——NuPlayer

通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解

【Appium】测试时遇到手机内嵌H5页面的切换问题

Qt读写XML文件

Qt编译QtXlsx库

idea:使用easyYapi插件导出yapi接口

Qt利用QtXlsx操作excel文件

Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code

CGM优化血糖监测管理——移宇科技亮相四川省国际医学交流促进会

PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
随机推荐
Comparison of indoor positioning methods of several intelligent robots
mysql查询字符串类型的字段使用数字类型查询时问题
How to read books and papers
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
Discussion on ES6 tail tune optimization
js将树形结构数据转为一维数组数据
stm32以及freertos 堆栈解析
[go] common concurrency model [generic version]
如何保护开源项目免遭供应链攻击-安全设计(1)
剑指offer day24 数学(中等)
An article understands variable lifting
CSV column extract column extraction
Comparison of indoor positioning technology
2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
编译原理题-带答案
ATSS(CVPR2020)
LeetCode简单题之三除数
form中enctype属性
Distributed service governance Nacos
Qt利用QtXlsx操作excel文件