当前位置:网站首页>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
边栏推荐
- 2022.4.11-4.17 AI行业周刊(第93期):AI行业的困局
- freertos学习02-队列 stream buffer message buffer
- Qt编译QtXlsx库
- Data deletion and modification (MySQL)
- One click cleanup of pycharm and jupyter cache files under the project
- js常用数组方法
- ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
- 程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
- [learning] audio and video development from scratch (9) -- nuplayer
- 跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
猜你喜欢
随机推荐
nn.Module类的讲解
form中enctype属性
WordPress爱导航主题 1.1.3 简约大气网站导航源码网址导航源码
数据的删除和修改操作(mysql)
编译原理题-带答案
C outputs a two-dimensional array with the following characteristics.
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
word加水印
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
Generate and parse tokens using JWT
Install MySQL for Ubuntu and query the average score
有意思的js 代码
Rotation function of leetcode medium problem
[go] common concurrency model [generic version]
CSV column extract column extraction
396. Rotate Function
监控智能回放是什么,如何使用智能回放查询录像
室内定位技术对比
Idea: export Yapi interface using easyyapi plug-in
线程的调度(优先级)