473,395 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Assigning between arrays by value

Any neat way to copy a snapshot of one array to another?

A normal assignment results in the second array pointing to the first,
with changes to either array affecting both.

As a trivial example:

var a=new Array();
a[0]="zero";
var b=a;
b[1]="one";
alert("a="+a.join("*")+String.fromCharCode(10)+"b= "+b.join("*"));

.... this results in a and b being identical two-element arrays.

Is there any easy way to set array (b) to be a copy of (a) BY VALUE -
ie using the contents of (a) as they were at the moment of assignment,
but leaving the two arrays separate so that subsequent changes to one
array does not affect the other?

John Geddes
England
Jul 20 '05 #1
3 3366
jo**@starmarkassociates.co.uk (John Geddes) writes:
Any neat way to copy a snapshot of one array to another?
There are some.
A normal assignment results in the second array pointing to the first,
with changes to either array affecting both.


You can do it manually:
---
function cloneArray(arr) {
var newArr = new Array();
for (var i=0;i<arr.length;i++) {
newArr[i]=arr[i];
}
return newArr;
}
---
Or you can use one of the methods that work on parts of the array:

var newArr = arr.slice(0,arr.length);
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <70**************************@posting.google.com >, seen
in news:comp.lang.javascript, John Geddes <jo**@starmarkassociates.co.uk
posted at Mon, 22 Sep 2003 07:22:35 :- Any neat way to copy a snapshot of one array to another?


A = [1, 2, 3]
C = [].concat(A)
C[1] = 4
x = [A, ' ', C] ; alert(x) // 1,2,3, ,1,4,3

But note

A = [1, 2, [5,6,7]]
C = [].concat(A)
C[1] = 4 ; C[2][1] = 9 // 1,2,5,9,7, ,1,4,5,9,7

though

A = [1, 2, [5,6,7]]
C = [].concat(A)
C[1] = 4 ; C[2] = 8 // 1,2,5,6,7, ,1,4,8

The "top level" of A is copied, but not lower levels.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3
Can you alter the Array prototype and add a clone or deepclone method?
They could then handle inner arrays however you wanted.
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ched | last post by:
Hi, I am performing a MySQL SELECT (which returns multiple rows) to $result and then extracting the results with mysql_fetch_array($result). I then want to build a number of arrays using the...
4
by: Bill | last post by:
I have a catalog of books which need to be categorized into different groups. Some of the books can be listed under more than one category. I'm creating a page where I can assign a group of books...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
0
by: dd | last post by:
Hi, I have the following code and want to assign value to a 3D arrays and print it out. But it seems that the value 1 hasn't been assigned to that array. Could anyone help me out? Thank you very...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
3
by: Donos | last post by:
Hello I have the following program, void getValue(unsigned char* pVal) { pVal = allValues(); } unsigned char* allValues()
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.