shadedecho
Programmer
anyone know of a simple straightforward way of copying an array (of n dimensions) to another variable, NOT just copying the reference to it.
I create an array of more than one dimension, with associative indexes, like so:
now, I want to be able to make a copy of that array over to a variable called blah2.
This actually appears to be copying a reference, because if I then execute:
I would expect it to have only affected the copy of the original, not the original. But this is not the case. The original is also affected.
so, then I though I could do something like:
Unfortunately, though the documentation and some online examples seem to suggest this will work, it does not, it appears to only copy the first dimension over. Also, it seemed like maybe associative references were not copied/respected in that copy.
So, I being a very (re)invent-the-wheel kinda guy, decided to write a recursive function that would do a generic copy of an array of any dimension depth. This is what I came up with, and so far it seems like it works, though it's admittedly a little dirty.
My question is this.... isn't there any better way to do this? This seems like the brute-force way of doing it, by creating a whole new array bit by bit, and force-copying each element into it. Surely javascript has a better way?
BTW, I originally thought it would be nice and elegant to have this be an object-oriented method that I could assign to the array object that let it duplicate itself.
Problem is that my implementation (more by necessity than design) is recursive, so to make the recursive calls would require assigning the method to each branch and dimension-level of the source array first (or the method could do it easily enough, but then that method WOULDN'T be leaving the source array alone, it would be modifying each of the sub-dimensions of the array). So I abandoned the OO approach and did a straight function reference approach.
I create an array of more than one dimension, with associative indexes, like so:
Code:
var blah = new Array();
blah["abc"] = new Array();
blah["abc"]["def"] = 1;
blah["abc"]["ghi"] = 2;
blah["def"]["ghi"] = 1;
blah["def"]["jkl"] = new Array();
blah["def"]["jkl"][0] = 1;
blah["def"]["jkl"][1] = 3;
now, I want to be able to make a copy of that array over to a variable called blah2.
Code:
var blah2 = blah;
This actually appears to be copying a reference, because if I then execute:
Code:
delete blah2["def"];
I would expect it to have only affected the copy of the original, not the original. But this is not the case. The original is also affected.
so, then I though I could do something like:
Code:
var blah2 = new Array(blah);
Unfortunately, though the documentation and some online examples seem to suggest this will work, it does not, it appears to only copy the first dimension over. Also, it seemed like maybe associative references were not copied/respected in that copy.
So, I being a very (re)invent-the-wheel kinda guy, decided to write a recursive function that would do a generic copy of an array of any dimension depth. This is what I came up with, and so far it seems like it works, though it's admittedly a little dirty.
Code:
function copyof(arrayObj) {
var temp = null;
if ((typeof arrayObj == "object") && (arrayObj.length > 0)) {
temp = new Array();
for (var index in arrayObj) {
if (typeof arrayObj[index] == "object") {
temp[index] = copyof(arrayObj[index]);
}
else temp[index] = arrayObj[index];
}
}
return temp;
}
My question is this.... isn't there any better way to do this? This seems like the brute-force way of doing it, by creating a whole new array bit by bit, and force-copying each element into it. Surely javascript has a better way?
BTW, I originally thought it would be nice and elegant to have this be an object-oriented method that I could assign to the array object that let it duplicate itself.
Code:
blah.prototype.copyof = copyof;
var blah2 = blah.copyof();
Problem is that my implementation (more by necessity than design) is recursive, so to make the recursive calls would require assigning the method to each branch and dimension-level of the source array first (or the method could do it easily enough, but then that method WOULDN'T be leaving the source array alone, it would be modifying each of the sub-dimensions of the array). So I abandoned the OO approach and did a straight function reference approach.