3

I imagine this is an easy thing to do, but I wasnt able to find the information I was looking for through google. I have popupProperties which is just default stuff. I then call to the service which returns specific overrides depending on the popup. How can I iterate through all of the service's overrides and apply them to the popupProperties?

var popupProperties = getDefaultPopupProperties();
var popupOverrides= popupService.getPopupOverrides(currPopupId);

angular.forEach(popupOverrides, function(popupProperty, propertyName){
    //replace defaults with popupData's properties
});
jensengar
  • 6,117
  • 17
  • 58
  • 90

2 Answers2

13

You should have a look at the solution of Josh David Miller which uses the extend method of angular (documentation).

var defaults = {name:'John',age:17,weight:55};
var overrides = {name:'Jack',age:28,color:'brown'};
var props = angular.extend(defaults, overrides);

// result
props: {
    name:'Jack',
    age:28,
    weight:55,
    color:'brown'
}

The values are copied in the defaults variable. There is no need of using the return value (var props =).

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256
jpmorin
  • 6,008
  • 2
  • 28
  • 39
1

I presume you mean both functions are returning objects with a number of properties (as opposed to an array).

If so, the following should work - just JavaScript, nothing AngularJS specific:

for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }

See this question for more details How can I merge properties of two JavaScript objects dynamically?

Community
  • 1
  • 1
Alex Osborn
  • 9,831
  • 3
  • 33
  • 44