Recursive/deep extend/assign in Underscore.js?

后端 未结 6 1367
遥遥无期
遥遥无期 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:27

    No, Underscore will not contain a deep extend since it's too complicated to deal with different types of objects. Instead, users are encouraged to implement their own solutions with the support for what they need.

    In your case it's only plain objects, so an implementation is quite straightforward:

    _.deepObjectExtend = function(target, source) {
        for (var prop in source)
            if (prop in target)
                _.deepObjectExtend(target[prop], source[prop]);
            else
                target[prop] = source[prop];
        return target;
    }
    

提交回复
热议问题