0

I feel like this may involve strings and a forEach statement? I'm pretty new to angular, but basically what I would like to do is this:

I have two objects, where every variable in object 1 is in object 2, but object 2 has more variables than object 1. I need to set all the values that the two share to be equal.

My guess would be perhaps going through Object 1 using forEach and setting the names of its variables to a string, then setting Object2.nameOfVariable equal to the item that the iterator is currently on?

This is a mockup that made sense in my head, but I have a poor understanding of forEach, and thus this does not work:

(in a factory)

loadValues: function(object1, object2) {
      object1.forEach(item){
        var varName = angular.toString(item);
        object2.varName = item;
      }
      return object1;
    },
Ajv2324
  • 422
  • 8
  • 24
  • Hard to tell eactly what you're trying to achieve. Possible duplicate of http://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically – mz3 Jul 15 '15 at 15:58
  • Sorry, I'm having trouble describing my goal. Basically, object1 is a subset of object2. I need to set all of the values that object2 has in common with object1 to be equal to object1's values, while maintaining the additional objects that object2 has. – Ajv2324 Jul 15 '15 at 16:01

1 Answers1

1

Loop over object1 and set its values to object2 values:

for (var prop in object1) {
    if (object2.hasOwnProperty(prop)) {
        object1[prop] = object2[prop];
    }
}
tymeJV
  • 103,943
  • 14
  • 161
  • 157