angularjs - extend recursive

前端 未结 6 1529
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 10:59

I would like to extend some properties recursive (aka. deep copy). much like jQuery does. I\'m not including jquery only b/c of one thing.

jQuery.extend( tru         


        
相关标签:
6条回答
  • 2020-12-10 11:20

    Building on Ryan's code, you can shorten the object check and you should also NOT extend functions so you don't override object pointers.

    var extendDeep = function extendDeep(dst) {
        angular.forEach(arguments, function(obj) {
            if (obj !== dst) {
                angular.forEach(obj, function(value, key) {
                    if (dst[key] && angular.isObject(dst[key])) {
                        extendDeep(dst[key], value);
                    } else if(!angular.isFunction(dst[key])) {
                        dst[key] = value;
                    }
                });
            }
        });
        return dst;
    };
    
    0 讨论(0)
  • 2020-12-10 11:31

    Here is an extendDeep function based off of the angular.extend function. If you add this to your $scope, you would then be able to call

    $scope.meta = $scope.extendDeep(ajaxResponse1.myMeta, ajaxResponse2.defaultMeta);
    

    and get the answer you are looking for.

    $scope.extendDeep = function extendDeep(dst) {
      angular.forEach(arguments, function(obj) {
        if (obj !== dst) {
          angular.forEach(obj, function(value, key) {
            if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
              extendDeep(dst[key], value);
            } else {
              dst[key] = value;
            }     
          });   
        }
      });
      return dst;
    };
    

    Note: This function has the side-effect of copying values from later arguments into the earlier arguments. For a simple fix to this side effect, you can change dst[key] = value to dst[key] = angular.copy(value).

    0 讨论(0)
  • 2020-12-10 11:31

    Angular has a copy method:

    angular.copy

    0 讨论(0)
  • 2020-12-10 11:34

    All the answers here are valid for versions of Angular before 1.4

    As of Angular 1.4, you can use angular.merge to do exactly that:

    Unlike extend(), merge() recursively descends into object properties of source objects, performing a deep copy.

    https://docs.angularjs.org/api/ng/function/angular.merge

    0 讨论(0)
  • 2020-12-10 11:35

    The same solution as Ryan but with support for array merge

    function extendDeep(dst) {
          angular.forEach(arguments, function (obj) {
              if (obj !== dst) {
                angular.forEach(obj, function (value, key) {
                    if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
                      extendDeep(dst[key], value);
                    } else if (dst[key] && dst[key].constructor && dst[key].constructor === Array) {
                      dst[key].concat(value);
                    } else if(!angular.isFunction(dst[key])) {
                      dst[key] = value;
                    }
                  }
                );
              }
            }
          );
          return dst;
        }
    
    0 讨论(0)
  • 2020-12-10 11:43
    function deepExtend(destination, source) {
      for (var property in source) {
        if (source[property] && source[property].constructor &&
         source[property].constructor === Object) {
          destination[property] = destination[property] || {};
          arguments.callee(destination[property], source[property]);
        } else {
          destination[property] = source[property];
        }
      }
      return destination;
    }
    

    Plunker

    Src: https://gist.github.com/gregdangelo/2343158

    0 讨论(0)
提交回复
热议问题