0

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?

7
  • Technically there are such things as associative arrays in javascript: {}. Commented Jul 11, 2012 at 9:16
  • @davin: technically they don't even have non-tricky ways of counting elements :-) Commented Jul 11, 2012 at 9:20
  • and I suppose you want the array methods to work on the new object as well? Commented Jul 11, 2012 at 9:21
  • @zerkms, that's not usually a requirement in the definition (cf. CLRS). Not to mention that Object.keys({...}).length isn't all that tricky. Commented Jul 11, 2012 at 9:24
  • @nbrooks - the array methods do work. And it seems I underestimated the $.extend - apparently if you deep copy the array, it works as advertised Commented Jul 11, 2012 at 9:27

3 Answers 3

1

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 length property will only count the indexed elements (not the properties), but for(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 loop for(var i = 0; i < assoc_array.length; i++)....

Anyway, a copy function like this will work for your purposes:

function copy_assoc(arr)
{
    var out = [];

    for(var key in arr)
    {
        if(!arr.hasOwnProperty(key))
        {
            continue;
        }

        out[key] = arr[key];
    }

    return out;
}

Here's a demonstration of it being used: http://jsfiddle.net/DbVV8/3/

Sign up to request clarification or add additional context in comments.

Comments

0

First off, if you want an associative array you should use an object:

var x = {};
x[0] = 1;
x[1] = 2;
x[2] = 3;
x.someProperty = 'Foo';

Then if you want a copy you could use jQuery's each function to iterate over each property and build a copy:

var copy = {};
$.each(x,function(i,e){
   copy[i] = e;
});

Live example: http://jsfiddle.net/KhBC9/

4 Comments

Thank you, but I really do need an array, Besides, why the $each? $extend is better
You realise in JS there is pretty much no difference between an object and an array? Plus why would you use extend when you're not extending the array, just copying it?
There may not be much difference, but since I'm using this to plot charts with d3.js, it has to be an array. The extra properties need to be there for other sections of the code to identify sets and so on
An array has the length property, among other things. It will only count the numerical indexes though
0

Let me answer my own question - For all my purposes - a deep clone of the array using $.extend(true, [], myArray) gives me a new array with all my properties intact. But I like @CodeMonkey's answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.