Merging objects (associative arrays)

前端 未结 16 1217
野的像风
野的像风 2020-12-04 08:20

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

相关标签:
16条回答
  • 2020-12-04 09:05

    I needed a deep-object-merging. So all of the other answers didn't help me very much. _.extend and jQuery.extend do well, unless you have a recursive array like i do. But it ain't so bad, you can program it in five minutes:

    var deep_merge = function (arr1, arr2) {
        jQuery.each(arr2, function (index, element) {
            if (typeof arr1[index] === "object" && typeof element === "object") {
                arr1[index] = deep_merge(arr1[index], element);
            } else if (typeof arr1[index] === "array" && typeof element === "array") {
                arr1[index] = arr1[index].concat(element);
            } else {
                arr1[index] = element;
            }
        });
        return arr1;
    }
    
    0 讨论(0)
  • 2020-12-04 09:06

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

    0 讨论(0)
  • 2020-12-04 09:09

    Rolling Your Own Extend/Mixin Function

    function extend(objects) {
        var args
            , first = Array.prototype.slice.call(arguments, 0, 1)[0]
            , second;
    
        if (arguments.length > 1) {
            second = Array.prototype.splice.call(arguments, 1, 1)[0];
            for (var key in second) {
                first[key] = second[key];
            }
            args = Array.prototype.slice.call(arguments, 0);
            return extend.apply(this, args);
        }
    
        return first;
    }
    

    ...

    var briansDirections = {
        step1: 'Remove pastry from wrapper.',
        step2: 'Place pastry toaster.',
        step3: 'Remove pastry from toaster and enjoy.',
    };
    extend(briansDirections, { step1: 'Toast Poptarts' }, { step2: 'Go ahead, toast \'em' }, { step3: 'Hey, are you sill reading this???' });
    

    ...

    This simply extends a splat of objects, recursively. Also, note that this recursive function is TCO (Tail-Call Optimized) as its return is the last call to itself.

    Additionally, you may want targeted properties. In this case, you may want to condense objects based upon id, quantity, or another property. This approach could have a small book written about it and requires object-juxtaposition and can get very complex. I've written a small library for this which is available upon request.

    Hope this helps!

    0 讨论(0)
  • 2020-12-04 09:09

    To merge arrays in jQuery what about $.merge?

    var merged = $.merge([{id:3, value:'foo3'}], [{id:1, value:'foo1'}, {id:2, value:'foo2'}]);
    merged[0].id == 3;
    merged[0].value == 'foo3';
    merged[1].id == 1;
    merged[1].value == 'foo1';
    merged[2].id == 2;
    merged[2].value == 'foo2';
    
    0 讨论(0)
提交回复
热议问题