Angularjs - copy specific properties from one object to another

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 function $scope.update ; I want to write something that should copy " only " firstname , middlename and lastname from $scope.Profile to $scope.BillingDetails .

I tried angular.copy and angular.extend , but

  • angular.extend combines $scope.BillingDetails and $scope.Profile . So I get the email and cellphone properties in $scope.BillingDetails as well, which I don't want.

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

What I need is for my update function to make $scope.BillingDetails equal to below the object:

 { 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 mentioned only 5-6 properties. In fact, I have to deal with more than 20 properties, and they are all dynamic. Therefore, for me this will not work by copying the firstname , middlename and lastname from Profile to BillingDetails . What can I do?

+7
javascript angularjs angularjs-service angularjs-scope angularjs-directive
source share
3 answers

You might be lucky 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

+12
source share

Simple Just assign them as follows:

 $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 come up with an easier way to copy multiple properties from one object to another.

Since you need to copy more than 3 properties, you can try the following:

 $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 an 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']); 
+4
source share

In fact, are you trying to update BillingDetails Profile values, for properties that they both have in common law?

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

 $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]; } } } 
+3
source share

All Articles