Q:

Merge hash with properties arrays

I have 2 hashes(objects) for ex.
hash1 = {myKey: ["string1"]}

hash2 = {myKey: ["string2"]}

I want to merge them together, so in the end i would get something like following - 
{myKey: ["string1", "string2"] }

I tried $.extend, but that doesnt work for arrays as property

A:

You can take a function for that.

function add(o, v) {
    Object.keys(v).forEach(function (k) {
        o[k] = o[k] || [];
        v[k].forEach(function (a) {
            o[k].push(a);
        });
    });
}


var hash1 = { myKey: ["string1"] },
    hash2 = { myKey: ["string2"] };

add(hash1, hash2);
document.write('<pre>' + JSON.stringify(hash1, 0, 4) + '</pre>');

