8

I've the following piece of code for copying one associative array to other,

<script>

    var some_db = new Array();

    some_db["One"] = "1";

    some_db["Two"] = "2";

    some_db["Three"] = "3";

    var copy_db = new Array();

    alert(some_db["One"]);

    copy_db = some_db.slice();

    alert(copy_db["One"]);

</script>

But the second alert says "undefined".. Am I doing something wrong here? Any pointers please...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Naveen
  • 1,040
  • 4
  • 15
  • 38
  • 5
    There are no associative arrays in javascript. – Niko Apr 22 '12 at 18:16
  • 6
    JavaScript arrays don't work with non-numerical keys. That's why `.slice` does not pick them up. Use a plain object instead and then look at [What is the most efficient way to clone a JavaScript object?](http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object). – Felix Kling Apr 22 '12 at 18:17
  • 1
    @Niko: Arrays are objects from the beginning... they are not "turned". – Felix Kling Apr 22 '12 at 18:19
  • @FelixKling Thanks, I missed that. Corrected my comment. – Niko Apr 22 '12 at 18:20
  • @Peter - You really should accept some answers on your questions before asking more of them. – gnarf Apr 22 '12 at 18:31
  • Thanks folks for the reply. I've changed my code to Object and is working fine now.. @gnarf Sorry was not aware of +1 here. Will take care from now on :) – Naveen Apr 23 '12 at 06:05

4 Answers4

18

In JavaScript, associative arrays are called objects.

<script>
    var some_db = {
       "One" : "1",
       "Two" : "2",
       "Three" : "3"
    };

    var copy_db = clone(some_db);

    alert(some_db["One"]);

    alert(copy_db["One"]);

    function clone(obj) {
        if (null == obj || "object" != typeof obj) return obj;
        var copy = obj.constructor();
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
        }
        return copy;
    }
</script>

I would normally use var copy_db = $.extend({}, some_db); if I was using jQuery.

Fiddle Proof: http://jsfiddle.net/RNF5T/

Thanks @maja.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iambriansreed
  • 21,935
  • 6
  • 63
  • 79
4

As @Niko says in the comment, there aren't any associative arrays in JavaScript.

You are actually setting properties on the array object, which is not a very good idea. You would be better off using an actual object.

var some_db = {};
some_db["One"] = "1";
some_db["Two"] = "2";
some_db["Three"] = "3";

var copy_db = {}, prop;
// Loop over all the keys in the object
for (prop in some_db) {
  // Make sure the object has this value, and not its prototype
  if (some_db.hasOwnProperty(prop)) {
    copy_db[prop] = some_db[prop];
  }
}

Many libraries implement an extend function which does exactly this (copy keys from one object to another). Most notably jQuery and Underscore.js. Underscore.js also has _.clone(obj) which is effectively _.extend( {}, obj )

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gnarf
  • 105,192
  • 25
  • 127
  • 161
4

If you want to use JSON, you can take this 'associative array' object:

var assArray = {zero:0, one:1, two:2, three:3, what:'ever', you:'want'};

And 'clone' it like this:

var clonedObj = JSON.parse(JSON.stringify(assArray));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

underscore.clone could help. It performs a shallow copy to the dictionary object or array.

var some_db = {
  "One" : "1",
  "Two" : "2",
  "Three" : "3"
};

copy_db = _.clone(some_db);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jichi
  • 6,333
  • 1
  • 31
  • 25