Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge non-existing fields #522

Closed
hriiss opened this issue Nov 19, 2024 · 4 comments
Closed

Merge non-existing fields #522

hriiss opened this issue Nov 19, 2024 · 4 comments

Comments

@hriiss
Copy link

hriiss commented Nov 19, 2024

I am wondering if this is possible, as I haven't seen it in the docs.
I want to merge 2 objects y1 and y2.
If I do:
deepmerge(y1, y2)
and y2 has some fields that don't exist in y1, it wont override or add them at all.
I am wondering if it's possible to change this.
Thank you.

@hriiss hriiss added Status: Triage This issue needs to be triaged. Type: Idea Marks an idea, which might be accepted and implemented. labels Nov 19, 2024
@RebeccaStevens
Copy link
Owner

RebeccaStevens commented Nov 20, 2024

The result of the merge should have all the fields of y1 and y2.
If you want y1 to be modified to have the fields of y2, you need to use deepmergeInto

const x = deepmerge(y1, y2);

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-nx5sp7

deepmergeInto(y1, y2);

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-t9y227

@RebeccaStevens RebeccaStevens added Status: Awaiting Response Issue or PR awaits response from the creator. Resolution: Not Reproducible The bug could not be reproduced. and removed Status: Triage This issue needs to be triaged. Type: Idea Marks an idea, which might be accepted and implemented. labels Nov 20, 2024
@hriiss
Copy link
Author

hriiss commented Nov 24, 2024

The result of the merge should have all the fields of y1 and y2. If you want y1 to be modified to have the fields of y2, you need to use deepmergeInto

const x = deepmerge(y1, y2);

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-nx5sp7

deepmergeInto(y1, y2);

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-t9y227

If I merge y1 and y2, I want to overwrite the fields in y1 with the existing fields of y2.
Any other field that doesn't exist should be added from y1.
Is this somehow possible?

Example:

const y1 = {
  field1: "value1",
  field2: {
    a: 0,
    b: 1
  }
}

const y2 = {
  field1: "newvalue",
  field3: "newvalue2"
}

const newObject = deepmerge(y1, y2);

Output:
{
  field1: "newvalue",
  field2: {
    a: 0,
    b: 1
  },
  field3: "newvalue2"
}

@RebeccaStevens
Copy link
Owner

That's exactly how it works:

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-nx5sp7

@RebeccaStevens RebeccaStevens closed this as not planned Won't fix, can't repro, duplicate, stale Nov 25, 2024
@RebeccaStevens RebeccaStevens removed Status: Awaiting Response Issue or PR awaits response from the creator. Resolution: Not Reproducible The bug could not be reproduced. labels Nov 25, 2024
@hriiss
Copy link
Author

hriiss commented Nov 25, 2024

That's exactly how it works:

https://codesandbox.io/p/sandbox/deepmerge-ts-example-forked-nx5sp7

Thank you for showing me an example.
It seems like there was an error on my side with how I fetched the object I wanted to merge into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants