Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Date property cannot be merged through "angular.merge" #11974

Closed
@shaunxu

Description

@shaunxu

Assuming I have the code.

$scope.obj1 = {
  name: 'Shaun',
  birthDate: new Date(1979, 5, 27)
};
$scope.obj2 = {
  name: 'Shaun Xu'
};
$scope.result = angular.merge({}, obj1, obj2);

I think $scope.result should be

{
  name: 'Shaun Xu',
  birthDate: new Date(1979, 5, 27)
}

But I got actually

{
  name: 'Shaun Xu',
  birthDate: {}
}

I reviewed the source code and found this is because, when performed baseExtend it will create an empty object property to destination when source property is object, this is case birthDate is Date and also Object. But later when perform baseExtend inside birthDate there will be nothing to copy since Date object doesn't have any enumerable member.

I think you should modify baseExtend, if the source property is Date, just copy the value to destination rather than performing deep copy.

function baseExtend(dst, objs, deep) {
  ... ...
  if (deep && isObject(src) && !angular.isDate(src)) {
    if (!isObject(dst[key])) dst[key] = isArray(src) ? [] : {};
    baseExtend(dst[key], [src], true);
  } else {
    dst[key] = src;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions