9

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...

6
  • 5
    There are no associative arrays in javascript. Commented Apr 22, 2012 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?. Commented Apr 22, 2012 at 18:17
  • 1
    @Niko: Arrays are objects from the beginning... they are not "turned". Commented Apr 22, 2012 at 18:19
  • @FelixKling Thanks, I missed that. Corrected my comment. Commented Apr 22, 2012 at 18:20
  • @Peter - You really should accept some answers on your questions before asking more of them. Commented Apr 22, 2012 at 18:31

4 Answers 4

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.

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

4 Comments

@FelixKling Thanks duh. Fixed.
Notice, that the function does not copy child-objects
@iambriansreed No, it does not. You would need to call your clone-Function again, when assigning an attribute. See your updated fiddle: jsfiddle.net/85Gbr This would perform a deep-copy: jsfiddle.net/RNF5T
@maja - Good catch. Updated.
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 )

Comments

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));

Comments

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);

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.