HighDots Forums  

Assigning between arrays by value

Javascript JavaScript language (comp.lang.javascript)


Discuss Assigning between arrays by value in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
John Geddes
 
Posts: n/a

Default Assigning between arrays by value - 09-22-2003 , 09:22 AM






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

Reply With Quote
  #2  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Assigning between arrays by value - 09-22-2003 , 09:53 AM






john (AT) starmarkassociates (DOT) co.uk (John Geddes) writes:

Quote:
Any neat way to copy a snapshot of one array to another?
There are some.

Quote:
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 - lrn (AT) hotpop (DOT) com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #3  
Old   
Dr John Stockton
 
Posts: n/a

Default Re: Assigning between arrays by value - 09-22-2003 , 02:23 PM



JRS: In article <708430f3.0309220622.327c20bd (AT) posting (DOT) google.com>, seen
in news:comp.lang.javascript, John Geddes <john (AT) starmarkassociates (DOT) co.uk
Quote:
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.


Reply With Quote
  #4  
Old   
asdf asdf
 
Posts: n/a

Default Re: Assigning between arrays by value - 09-24-2003 , 01:38 PM



Can you alter the Array prototype and add a clone or deepclone method?
They could then handle inner arrays however you wanted.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.