1

I have an object in angular 5, like this one:

public obj1 = {
    A: 5,
    B: false,
    C: 'c'
};

And another one:

public obj2 = {
    A: 10,
    E: true,
    D: 'd'
};

How can I merge these two object, or delete one of these object properties ? I need to do Map operations over this simple object.


Update:

I need a way, if there is, to cast this simple object into a Map.

Ebraheem Alrabeea
  • 2,130
  • 3
  • 23
  • 42
  • Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – ConnorsFan Mar 20 '18 at 17:57

1 Answers1

2

Yes, you can.

public obj1 = {
    A: 5,
    B: false,
    C: 'c'
};

public obj2 = {
    A: 10,
    E: true,
    D: 'd'
};

const newItem = Object.assign({}, obj1, obj2);

See the first answer of this question.

Merge two objects with ES6

Rafael Ferreira
  • 166
  • 1
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/19173457) – Toby Speight Mar 20 '18 at 18:26
  • Ok, thank you for the explanation, in the next time I will post the better answer, thanks again. – Rafael Ferreira Mar 20 '18 at 19:50
  • 1
    You can (and should) [edit] your answer to improve it. – Toby Speight Mar 20 '18 at 23:11
  • That is a good way to do merge, But is there a way to delete a property from this object. – Ebraheem Alrabeea Mar 21 '18 at 14:37