Technically there are no such things as an Associative Array in Javascript. But for reasons I can’t avoid, I’ve ended up having t do things such as
var x = [1,2,3];
x.someRequiredProperty = 'some value';
As expected, Javascript being Javascript, it works. But now, how do I make a copy of this ‘array‘ so that I can work on multiple instances of this array without compromising the original?
I’ve tried jQuery $.extend([], x), which doesn’t really give me a new copy. Is there something I’m missing?
I don’t like the idea of an “associative” Javascript array, since adding properties to an array won’t make sense to JavaScript. For instance, the
lengthproperty will only count the indexed elements (not the properties), butfor(var x in assoc_array)will iterate across indexes of the array as well as the “keys”. So to loop just the array items you’d have to use a full for loopfor(var i = 0; i < assoc_array.length; i++)....Anyway, a copy function like this will work for your purposes:
Here’s a demonstration of it being used: http://jsfiddle.net/DbVV8/3/