17

I have these two objects:

obj1 = {a: '', b: ''}
obj2 = {a: '1', b: '2', c: '3'}

I want to copy all matching properties from obj2 to obj1. What is the best way of doing that in Typescript?

Sam Hanley
  • 4,633
  • 7
  • 35
  • 58
sreginogemoh
  • 9,074
  • 25
  • 83
  • 142
  • Possible duplicate of [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript) – Michael Freidgeim Oct 03 '19 at 07:27

3 Answers3

18

what is the best way of doing that in typescript

Same as in JavaScript. Just use Object.keys

The following code moves stuff from obj2 to obj1:

let obj1 = {a: '', b: ''}
let obj2 = {a: '1', b: '2', c: '3'}

Object.keys(obj2).forEach(key=>obj1[key]=obj2[key]);

For any condition like must not already be in obj1 etc you can do that check in the forEach

basarat
  • 234,454
  • 52
  • 420
  • 492
  • 2
    don't you think there must be some conditional statement to check if key exist in obj1 or not. e.g. obj1.hasOwnProperty(key); – Ajay Apr 06 '16 at 06:13
  • @basarat do you have the same **copy only matching properties** but a deep traversal? I am always facing this issue when loading user settings structure from some local storage. I would like to start with my latest object structure loaded with defaults and only copy the matching key's values recursively from the object loaded from storage. I cannot seem to find the right post for this. Thank you. – Meryan May 30 '20 at 04:13
  • The code in this answer is incomplete, as it ignores the OP's request to only copy matching properties. The following should be added: `if (key in obj1)`. – OfirD Jan 13 '21 at 22:01
  • 2
    Maybe TSC has become more strict, but I get a type error (ts:7053) for this code, complaining about there being no indexer on the object types. – Krisztián Balla May 15 '21 at 20:18
13

If you don't mind any keys of obj2 not exist in obj1, a clearer way is using Object.assign(obj1, obj2):

iStar
  • 1,074
  • 12
  • 20
1

I think @basarat meant to iterate over the target properties not the source ones like this

enter image description here

Actually he is testing for key matching it won't matter.

  • For performance we probably want to pick the shortest list of the keys.
Meryan
  • 950
  • 10
  • 20
  • 8
    Please post code as [text rather than images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) – DBS Oct 28 '20 at 14:10
  • 1
    This gives me ERROR ts:7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type. No index signature with a parameter of type 'string' was found on type. – Krisztián Balla May 15 '21 at 20:21