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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex