Recursive/deep extend/assign in Underscore.js?

后端 未结 6 1371
遥遥无期
遥遥无期 2020-11-29 10:47

Is there any way to get Underscore.js extend function:

Copy all of the properties in the source objects over to the destination object, and return t

6条回答
  •  半阙折子戏
    2020-11-29 11:45

    Stand-alone version of Bergi's deep extend, including the fix for when a value is a string instead of an object. Also patched to be more strict.

    function deepObjectExtend (target, source) {
        for (var prop in source) {
            if (source.hasOwnProperty(prop)) {
                if (target[prop] && typeof source[prop] === 'object') {
                    deepObjectExtend(target[prop], source[prop]);
                }
                else {
                    target[prop] = source[prop];
                }
            }
        }
        return target;
    }
    

提交回复
热议问题