2

I have two objects with the same properties but holding different values

EXAMPLE:

export class obj1{
  arr1: values[]
  dic1: { [key: string]: Value; };
  bool1: boolean
}
export class obj2{
  arr1: values[]
  dic1: { [key: string]: Value; };
  bool1: boolean
}

Where the property of the object is an array or dictionary I want obj2 values to add to the values of obj1. Where there are boolean\string\number properties I want obj2 properties to replace obj1

Is there a clean way of doing this in a few lines or will I have to map every property.

Richard Watts
  • 954
  • 2
  • 8
  • 21
  • 1
    Try this maybe if its arrays. `var newobj = Object.assign(obj1, obj2);` – Deckerz Aug 17 '17 at 15:23
  • You can use lodash [`_.mergeWith`](https://lodash.com/docs/4.17.4#mergeWith) and pass your function. See this: http://jsbin.com/xugujon/edit?html,js – Mosh Feu Aug 17 '17 at 15:38
  • Deckerz unfortunately that replaces all the properties with the same name, it doesn't add to arrays and dictionaries – Richard Watts Aug 17 '17 at 15:50
  • You should give a real example (that's what examples are for). Your example contains syntax errors. And tell us what result you expect. I'm not sure if you want/need a recursive function. I would if I saw an example. – Emmanuel Delay Aug 17 '17 at 15:52

1 Answers1

1

I asked a similar question here.

Basically:

Since your two classes have the same properties, you could just have them extend the same class. Then you could write a method for the class itself called "merge()" or something that maps the properties manually, which shouldn't actually look that complicated.

Edit:

Something similar to this, but you might still need separate classes for your use case. Edit: For merging dictionaries added the logic.

export class obj {
  arr1: values[]
  dic1: { [key: string]: Value; };
  bool1: boolean


  merge(otherObj : obj) {
    let newObj:obj;
    newObj.arr1 = this.arr1.concat(otherObj.arr1);
    newObj.dic1 = {};
    newObj.dic1 = Object.assign(newObj.dic1, this.dic1);
    newObj.dic1 = Object.assign(newObj.dic1, otherObj.dic1);
    newObj.bool1 = this.otherObj.bool1;
    return newObj;
  }
}

See:

How can I merge properties of two JavaScript objects dynamically?

mintychai
  • 281
  • 3
  • 10
  • Wouldn't this newObj.dic1 = this.otherObj.dic1; replace newObj.dic1 with otherObj.dic1? How would you add to the first dictionary – Richard Watts Aug 17 '17 at 15:46
  • @RichardWatts Yeah that's what I don't understand from your question, do you have an example of how you want these two dictionaries to be merged? I'm guessing you want all the key/value pairs to be all put into one dictionary, but what if the keys are the same on two objects? – mintychai Aug 17 '17 at 16:07
  • Good point mintychai. The two dictionaries will never have the same keys. I do want the key\value pairs to be all put into one dictionary – Richard Watts Aug 17 '17 at 16:11
  • @RichardWatts Merging dictionaries is pretty easy: https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically Added to the answer! – mintychai Aug 17 '17 at 16:24
  • from what I can see the solution in that post would overwrite values where the properties match. I want to merge so I maintain all values – Richard Watts Aug 18 '17 at 08:51
  • I thought you said they never have the same keys? – mintychai Aug 18 '17 at 14:33