Assigning between arrays by value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Geddes

    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.jo in("*")+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
  • Lasse Reichstein Nielsen

    #2
    Re: Assigning between arrays by value

    john@starmarkas sociates.co.uk (John Geddes) writes:
    [color=blue]
    > Any neat way to copy a snapshot of one array to another?[/color]

    There are some.
    [color=blue]
    > A normal assignment results in the second array pointing to the first,
    > with changes to either array affecting both.[/color]

    You can do it manually:
    ---
    function cloneArray(arr) {
    var newArr = new Array();
    for (var i=0;i<arr.lengt h;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@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Dr John Stockton

      #3
      Re: Assigning between arrays by value

      JRS: In article <708430f3.03092 20622.327c20bd@ posting.google. com>, seen
      in news:comp.lang. javascript, John Geddes <john@starmarka ssociates.co.uk[color=blue]
      > posted at Mon, 22 Sep 2003 07:22:35 :-[/color]
      [color=blue]
      >Any neat way to copy a snapshot of one array to another?[/color]

      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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      • asdf asdf

        #4
        Re: Assigning between arrays by value

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

        Comment

        Working...