angularjs - 将一个对象的公共属性复制到另一个对象

时间:2021-03-04 20:14:59

I have a controller like this:

我有一个像这样的控制器:

CheckoutController = function() {
    $scope.Profile = {
        firstname : 'Ruchir',
        middlename : 'Shakun',
        lastname : 'Gupta',
        email : 'ruchir@example.com',
        cellphone : '9876543210'
    }

    $scope.BillingDetails = {
        firstname : undefined,
        middlename : undefined,
        lastname : undefined,
        addressline : undefined,
        city : undefined,
        zipcode : undefined
    }

    $scope.update = function() {
        // I want to write some awesome code here as explained below
    }
}

Now, in the $scope.update function; I want to write something that should copy 'only common properties' i.e. firstname, middlename, and lastname from $scope.Profile to $scope.BillingDetails.

现在,在$ scope.update函数中;我想写一些应该复制'only common properties'的东西,即$ scope.Profile中的firstname,middlename和lastname到$ scope.BillingDetails。

I tried angular.copy and angular.extend but,

我试过angular.copy和angular.extend但是,

  • angular.extend merges $scope.BillingDetails and $scope.Profile. So I get email and cellphone properties in $scope.BillingDetails as well -- what I don't want.

    angular.extend合并$ scope.BillingDetails和$ scope.Profile。所以我在$ scope.BillingDetails中获得了电子邮件和手机属性 - 我不想要的。

  • angular.copy overwrites $scope.BillingDetails and I lose addressline, city and zipcode from $scope.BillingDetails -- what I don't want.

    angular.copy覆盖$ scope.BillingDetails,我从$ scope.BillingDetails丢失了地址,城市和邮政编码 - 我不想要的。

What I want my update function to do is it should make $scope.BillingDetails equal to below object:

我希望我的更新函数要做的是它应该使$ scope.BillingDetails等于下面的对象:

{
    firstname : 'Ruchir',
    middlename : 'Shakun',
    lastname : 'Gupta',
    addressline : undefined,
    city : undefined,
    zipcode : undefined    
}

This scenario is just an example. To shorten the length of my question, I have mentioned 5-6 properties only. In fact, I have to deal with more than 20 properties and all are dynamic. So it won't work for me by copying one-by-one properties firstname, middlename and lastname from Profile to BillingDetails. What can I do?

这种情况只是一个例子。为了缩短我的问题的长度,我只提到了5-6个属性。事实上,我必须处理超过20个属性,而且都是动态的。因此,通过将Profile中的逐个属性firstname,middlename和lastname复制到BillingDetails,它将无法工作。我能做什么?

3 个解决方案

#1


13  

You may have luck with something like this:

你可能会有这样的运气:

$scope.update = function() {
  _update($scope.Profile, $scope.BillingDetails);
}

function _update(srcObj, destObj) {
  for (var key in destObj) {
    if(destObj.hasOwnProperty(key) && srcObj.hasOwnProperty(key)) {
      destObj[key] = srcObj[key];
    }
  }
}

plunker

plunker

#2


4  

Simple. Just assign them like this:

简单。只需像这样分配它们:

$scope.update = function() {
    $scope.BillingDetails.firstname = $scope.Profile.firstname;
    $scope.BillingDetails.middlename = $scope.Profile.middlename;
    $scope.BillingDetails.lastname = $scope.Profile.lastname;
}

I really can't think of a more straightforward method of copying a couple of properties from one object to another.

我真的想不出一种将一些属性从一个对象复制到另一个对象的更直接的方法。

Since you need to copy more than 3 properties, you could try this:

由于您需要复制3个以上的属性,您可以尝试这样做:

$scope.update = function() {
    // Add the properties you want to copy to this array.
    var properties = ['firstname', 'middlename', 'lastname'];
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

Or, pass the array as a parameter:

或者,将数组作为参数传递:

$scope.update = function(properties) {
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

$scope.update(['firstname', 'middlename', 'lastname']);

#3


3  

In fact, you try to update BillingDetails with values of Profile, for properties they both have in common right?

实际上,您尝试使用Profile的值更新BillingDetails,以获取它们共有的属性吗?

If you can change the default values of BillingDetails with null instead of undefined, you can try this code:

如果您可以使用null而不是undefined更改BillingDetails的默认值,则可以尝试以下代码:

$scope.BillingDetails = {
    firstname : null,
    middlename : null,
    lastname : null,
    addressline : null,
    city : null,
    zipcode : null
}

$scope.update = function() {
    for(var key in $scope.Profile) {
        if(typeof $scope.BillingDetails[key] !== 'undefined') {
            $scope.BillingDetails[key] = $scope.Profile[key];
        }
    }
}

#1


13  

You may have luck with something like this:

你可能会有这样的运气:

$scope.update = function() {
  _update($scope.Profile, $scope.BillingDetails);
}

function _update(srcObj, destObj) {
  for (var key in destObj) {
    if(destObj.hasOwnProperty(key) && srcObj.hasOwnProperty(key)) {
      destObj[key] = srcObj[key];
    }
  }
}

plunker

plunker

#2


4  

Simple. Just assign them like this:

简单。只需像这样分配它们:

$scope.update = function() {
    $scope.BillingDetails.firstname = $scope.Profile.firstname;
    $scope.BillingDetails.middlename = $scope.Profile.middlename;
    $scope.BillingDetails.lastname = $scope.Profile.lastname;
}

I really can't think of a more straightforward method of copying a couple of properties from one object to another.

我真的想不出一种将一些属性从一个对象复制到另一个对象的更直接的方法。

Since you need to copy more than 3 properties, you could try this:

由于您需要复制3个以上的属性,您可以尝试这样做:

$scope.update = function() {
    // Add the properties you want to copy to this array.
    var properties = ['firstname', 'middlename', 'lastname'];
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

Or, pass the array as a parameter:

或者,将数组作为参数传递:

$scope.update = function(properties) {
    for(var i = 0; i < properties.length; i++){
         $scope.BillingDetails[properties[i]] = $scope.Profile[properties[i]];
    }
}

$scope.update(['firstname', 'middlename', 'lastname']);

#3


3  

In fact, you try to update BillingDetails with values of Profile, for properties they both have in common right?

实际上,您尝试使用Profile的值更新BillingDetails,以获取它们共有的属性吗?

If you can change the default values of BillingDetails with null instead of undefined, you can try this code:

如果您可以使用null而不是undefined更改BillingDetails的默认值,则可以尝试以下代码:

$scope.BillingDetails = {
    firstname : null,
    middlename : null,
    lastname : null,
    addressline : null,
    city : null,
    zipcode : null
}

$scope.update = function() {
    for(var key in $scope.Profile) {
        if(typeof $scope.BillingDetails[key] !== 'undefined') {
            $scope.BillingDetails[key] = $scope.Profile[key];
        }
    }
}