Skip to main content

Merging associative arrays javascript



What's the best/standard way of merging two associative arrays in javascript? Does everyone just do it by rolling their own for loop?





Source: Tips4all

Comments

  1. with jquery you can call $.extend

    obj1 = {a: 1, b: 2};
    obj2 = {a: 4, c: 110};

    obj3 = $.extend(obj1, obj2);

    obj1 == obj3 == {a: 4, b: 2, c: 110}


    (assoc. arrays are objects in js)

    look here: http://api.jquery.com/jQuery.extend/

    ReplyDelete
  2. This is how Prototype does it:

    Object.extend = function(destination, source) {
    for (var property in source) {
    if (source.hasOwnProperty(property)) {
    destination[property] = source[property];
    }
    }
    return destination;
    };


    called as, for example:

    var arr1 = { robert: "bobby", john: "jack" };
    var arr2 = { elizabeth: "liz", jennifer: "jen" };

    var shortnames = Object.extend(arr1,arr2);


    EDIT: added hasOwnProperty() check as correctly pointed out by bucabay in comments

    ReplyDelete
  3. In dojo, the 2-objects/arrays "merge" would be dojo.mixin(destination, source) -- you can also mix multiple sources into one destination, etc -- see the mixin function's reference for details.

    ReplyDelete
  4. In Javascript there is no notion of
    associative array, there are objects
    The only way to merge two objects is
    to loop for their properties and
    copy pointers to their values that
    are not primitive types and values
    for primitive types to another
    instance

    ReplyDelete
  5. do you want to overwrite a property if the names are the same but the values are not?

    And do you want to permanently change one of the original objects,

    or do you want a new merged object returned?

    function mergedObject(obj1, obj2, force){
    for(var p in obj1) this[p]= obj1[p];
    for(var p in obj2){
    if(obj2.hasOwnProperty(p)){
    if(force || this[p]=== undefined) this[p]= obj2[p];
    else{
    n= 2;
    while(this[p+n]!== undefined)++n;
    this[p+n]= obj2[p];
    }
    }
    }
    }

    ReplyDelete
  6. Underscore also has an extend method:


    Copy all of the properties in the source objects over to the
    destination object. It's in-order, so the last source will override
    properties of the same name in previous arguments.


    _.extend(destination, *sources)

    _.extend({name : 'moe'}, {age : 50});
    => {name : 'moe', age : 50}

    ReplyDelete
  7. Yahoo UI (YUI) also has a helper function for this:

    http://developer.yahoo.com/yui/examples/yahoo/yahoo_merge.html

    YAHOO.namespace('example');

    YAHOO.example.set1 = { foo : "foo" };
    YAHOO.example.set2 = { foo : "BAR", bar : "bar" };
    YAHOO.example.set3 = { foo : "FOO", baz : "BAZ" };

    var Ye = YAHOO.example;

    var merged = YAHOO.lang.merge(Ye.set1, Ye.set2, Ye.set3);

    ReplyDelete
  8. Keep it simple...

    function mergeArray(array1,array2) {
    for(item in array1) {
    array2[item] = array1[item];
    }
    return array2;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...