Forums > Javascript

copying arrays

March 12, 2006 | 11:32 am

hi,
i need to copy one array into another.
doing something like:
myArr1 = myArr2;
will just pass the reference, not copying the data, and further
changes to myArr2 will be reflected in myArr1 – not want i need.

iterating through the whole array and copying value by value works,
but seems a little long winded (maybe it isn’t?)
for(i=0;i
myArr1[i] = myArr2[i];

was looking around for something like "Array.copy()" – but no luck.
came up with:
myArr1 = myArr2.slice(0);
to actually copy the data, and it works.

just wanted to know what others are doing in this situation and if
there is a preferred method.
thanks,
volker.


June 10, 2006 | 12:59 am

those are good tips! I looked through the js core guide, and there simply doesn’t seem to be a method for copying an array to a new one. go figure!

PeterN


June 10, 2006 | 2:35 am

On Jun 9, 2006, at 5:59 PM, pnyboer wrote:

> those are good tips! I looked through the js core guide, and there
> simply doesn’t seem to be a method for copying an array to a new
> one. go figure!

For numerically arrays, just walk one array from zero to array.length
copying one element to the other:

function copyArray(src)
{
var dest = new Array();
var i;
var len = src.length;

for (i=0;i
dst[i] = src[i];

return dst;
}

For associative arrays (i.e. key/value maps, also knows as hash
tables, dictionaries, or hash arrays, and the way in which Andrew
Pask suggested you use arrays to store dynamically named arrays),
people recommend using Object in place of Array. However, if you
aren’t extending Array.prototype (which often is not the case, at
least as used in Max), you can safely use the Array class in this
way. For this type of iteration use the Javascript "for … in"
construct to iterate across an object’s properties. Here’s a
description of why you may wish to use Object in place of Array for
associative arrays:

http://www.andrewdupont.net/2006/05/18/javascript-associativ e-arrays-
considered-harmful/

function copyObject(src)
{
var dest = new Object();
var i;

for (i in src)
dst[i] = src[i];

return dst;
}

Hope this helps.

-Joshua


June 12, 2006 | 5:02 am

Quote: pnyboer wrote on Sat, 10 June 2006 12:59
—————————————————-
> those are good tips! I looked through the js core guide, and
> there simply doesn’t seem to be a method for copying an array
> to a new one.

Not as such.

newarray = oldarray.slice(0); or
newarray = oldarray.concat([]);

Pretty much the standard one-line methods back when I was doing much js web development. Still what I’m using anyway.

As Joshua notes, you could also add a .copy() method extending the Array prototype and bung that in a shared js library, for that matter, though I haven’t felt the need to do anything like this in Max (yet).

I do miss PHP’s true associative arrays (and variable variables for that matter) when I work with javascript, and have to resist the temptation to use faux-associative array objects and treat them as real arrays – as outlined in the linked site, myarray["key1"] is not an array element – forgetting that and using convenient array methods won’t produce the results expected. Yikes.


Viewing 4 posts - 1 through 4 (of 4 total)