22

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,707
  • 7
  • 35
  • 63
Sergino
  • 10,128
  • 30
  • 98
  • 159
  • 1
    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
  • 261,912
  • 58
  • 460
  • 511
  • 3
    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
  • 2
    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
  • 6
    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
  • TSC strictness is an option – bikeman868 Jun 01 '23 at 04:14
16

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

iStar
  • 1,112
  • 12
  • 20
2

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

function CopyMatchingKeyValues(Target, Source) {
    return Object.keys(Target).forEach(key => {
        if (Source[key] !== undefined)
            Target[key] = Source[key];
    });
}

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.
BoydB
  • 3
  • 2
Meryan
  • 1,285
  • 12
  • 25
  • 11
    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
  • 3
    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