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
angularjs - copy common properties from one object to another
提问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.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
函数中;我想写的东西,应该复制“唯一的共同特性”,即firstname
,middlename
和lastname
从$scope.Profile
到$scope.BillingDetails
。
I tried angular.copy
and angular.extend
but,
我试过了angular.copy
,angular.extend
但是,
angular.extend
merges$scope.BillingDetails
and$scope.Profile
. So I getemail
andcellphone
properties in$scope.BillingDetails
as well -- what I don't want.angular.copy
overwrites$scope.BillingDetails
and I loseaddressline
,city
andzipcode
from$scope.BillingDetails
-- what I don't want.
angular.extend
合并$scope.BillingDetails
和$scope.Profile
。所以,我得email
和cellphone
在性能$scope.BillingDetails
以及-我不想要什么。angular.copy
覆写$scope.BillingDetails
和我输了addressline
,city
而且zipcode
从$scope.BillingDetails
-我不想要什么。
What I want my update
function to do is it should make $scope.BillingDetails
equal 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
, middlename
and lastname
from Profile
to BillingDetails
.
What can I do?
这个场景只是一个例子。为了缩短我的问题的长度,我只提到了 5-6 个属性。事实上,我要处理20多个属性,而且都是动态的。所以它不会复制一个接一个性质对我来说有效firstname
,middlename
并lastname
从Profile
到BillingDetails
。我能做什么?
回答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];
}
}
}
回答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 BillingDetails
with values of Profile
, for properties they both have in common right?
实际上,您尝试BillingDetails
使用 的值进行更新Profile
,对于它们都有共同的属性,对吗?
If you can change the default values of BillingDetails with null
instead 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];
}
}
}