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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26340181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:50:00  来源:igfitidea点击:

angularjs - copy common properties from one object to another

javascriptangularjsangularjs-directiveangularjs-scopeangularjs-service

提问by Ruchir Gupta

I have a controller like this:

我有一个这样的控制器:

CheckoutController = function() {
    $scope.Profile = {
        firstname : 'Ruchir',
        middlename : 'Shakun',
        lastname : 'Gupta',
        email : '[email protected]',
        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.updatefunction; I want to write something that should copy 'only common properties' i.e. firstname, middlename, and lastnamefrom $scope.Profileto $scope.BillingDetails.

现在,在$scope.update函数中;我想写的东西,应该复制“唯一的共同特性”,即firstnamemiddlenamelastname$scope.Profile$scope.BillingDetails

I tried angular.copyand angular.extendbut,

我试过了angular.copyangular.extend但是,

  • angular.extendmerges $scope.BillingDetailsand $scope.Profile. So I get emailand cellphoneproperties in $scope.BillingDetailsas well -- what I don't want.

  • angular.copyoverwrites $scope.BillingDetailsand I lose addressline, cityand zipcodefrom $scope.BillingDetails-- what I don't want.

  • angular.extend合并$scope.BillingDetails$scope.Profile。所以,我得emailcellphone在性能$scope.BillingDetails以及-我不想要什么。

  • angular.copy覆写 $scope.BillingDetails和我输了addresslinecity而且 zipcode$scope.BillingDetails-我不想要什么。

What I want my updatefunction to do is it should make $scope.BillingDetailsequal to below object:

我希望我的update函数做的是它应该$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, middlenameand lastnamefrom Profileto BillingDetails. What can I do?

这个场景只是一个例子。为了缩短我的问题的长度,我只提到了 5-6 个属性。事实上,我要处理20多个属性,而且都是动态的。所以它不会复制一个接一个性质对我来说有效firstnamemiddlenamelastnameProfileBillingDetails。我能做什么?

回答by JcT

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

笨蛋

回答by Cerbrus

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']);

回答by Tip-Sy

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

实际上,您尝试BillingDetails使用 的值进行更新Profile,对于它们都有共同的属性,对吗?

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

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

$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];
        }
    }
}