当前位置:网站首页>Copy array in JS
Copy array in JS
2022-04-23 08:25:00 【beinlife】
JS Copy array in
var array1 = new Array("1","2","3");
var array2;
array2 = array1;
array1.length = 0;
alert(array2); // Return to empty
This is the wrong way , because javascript It is divided into original type and reference type ( And java、c# similar ).Array Is a reference class
type .array2 What you get is a quote , So for array1 The changes will affect array2.
You can use slice() replicate , because slice() Return is also an array .
var array1 = new Array("1","2","3");
var array2;
array2 = array1.slice(0);
array1.length = 0;
alert(array2); // return 1、2、3
Be careful concat() The returned is not the calling function Array, It's a new Array, So you can use this to replicate .
var array1 = new Array("1","2","3");
var array2;
array2 = array2.concat(array1);
array1.length = 0;
alert(array2); // return 1、2、3
http://blog.sina.com.cn/s/blog_51baa1570100io8r.html
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230734168920.html
边栏推荐
猜你喜欢

Failed to convert a NumPy array to a Tensor(Unsupported Object type int)

396. Rotate Function

2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry

QT compilation qtxlsx Library

Asan minimalism

LeetCode简单题之统计字符串中的元音子字符串

mysql查询字符串类型的字段使用数字类型查询时问题

clang 如何产生汇编文件

Listed on the Shenzhen Stock Exchange: the market value is 5.2 billion yuan. Lu is the East and his daughter is American

How to generate assembly file
随机推荐
【深度好文】Flink SQL流批⼀体化技术详解(一)
Qt读写XML文件
Protobuf简介
AQS & ReentrantLock 实现原理
ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建
js常用数组方法
队列(c语言/链表)
[effective go Chinese translation] part I
万物互联下如何对设备进行加密
There are some problems when using numeric type to query string type fields in MySQL
使用JWT生成与解析Token
Input / output system
npm安装yarn
室内定位技术对比
Qt编译QtXlsx库
colorui 解决底部导航遮挡内容问题
Queue (C language / linked list)
What is RPC
LeetCode简单题之重新排列日志文件
数据的删除和修改操作(mysql)