-
Notifications
You must be signed in to change notification settings - Fork 218
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
Arrays with similar items merge in an unexpected manner #14
Comments
… can target individual array items
Your behavior seems legit, if tests are ok submit a PR and ping @nrf110 |
I'm wondering if merging arrays is a good idea? I mean... it seems like a slippery slope. Might be more sane to just take the entire array from "object y". I think the current behaviour is correct. |
Oh wait... it does do it for digits. Hmmmm... I don't know if that makes sense. To me it should only be key/value merging. If you accept the "y" value of two matching keys when they have a string value... why merge them when they are arrays? What if one is a string and one is an array? |
I would like to see this issue resolved. I'm running into a similar issue:
I expect:
@basicallydan Can you create a pull request for your fork? EDIT: Confirmed that the fork works for my issue. |
Hey @oddityoverseer13, it does work indeed... I remember there was a reason I didn't make a PR, I forget exactly what it was though. I'll have a look outside of working hours this week :) |
Just add my 2 cents here... I would expect this tool to only merge objects and leave arrays alone. To me arrays are too ambiguous and force the tool to start making assumptions when combining them. I also think that if you have a properly designed object schema you would not really have the need to merge arrays. Rather you would expect one object's array value to trump another. Though I assume this tool is being used to merge data that is clearly defined. |
I agree with @howardroark, despite me being the person to open this up. It might be nice to make the array-behaviour ability based on an option, e.g. - should it add to the array, replace it entirely or replace selected indices. E.g. merge(meh1,meh2, { arrays: 'add' });
merge(meh1,meh2, { arrays: 'replace' });
merge(meh1,meh2, { arrays: 'replaceIndex' }); Something like that? |
@basicallydan That sounds like a good option. Give the users the flexibility to do what they'd like. |
If some jQuery users are interested I created a function similar to $.extend with configurable behaviour for arrays: https://github.com/mistic100/jQuery.extendext These behaviours are:
|
@basicallydan thanks for your fork, it worked for me. If you need a hand with the changes you proposed let me know, I can give you a hand. |
i was just surprised by this too. objects with the same name of type array i expected to concatenate and were replaced by the second one:
|
I've just tried to use deepmerge to merge my env-specific webpack configs:
for me it was completely unexpected behaviour |
Have the same issue here. Is there plans to resolve this or I need to create my own implementation? |
I don't think how array merges are currently implemented is incorrect. I also don't think concatenating arrays is incorrect behavior either. Both use cases are valid. That's why this is tricky. What makes the most sense to me is to allow the user to decide how they want their arrays handled. Adding a third options parameter to var obj1 = { a: [{ b: 'c' }] }
var obj2 = { a: [{ d: 'e' }] }
deepmerge(obj1, obj2) // { a: [{ b: 'c', d: 'e' }] } var obj1 = { a: [{ b: 'c' }] }
var obj2 = { a: [{ d: 'e' }] }
deepmerge(obj1, obj2, { arrays: 'concat' }) // { a: [{ b: 'c' }, { d: 'e' }] } |
or this |
Yes lodash deepmerge does nothing about array by default but you can do it by providing a custom fn that check if it's an array: https://lodash.com/docs#merge I think I was the one that did the "funny" array merge here, it really depends on your use case, that's why it's tricky as you said. Allowing an option and maybe reverting to the default that the first issue comment sayd would be better (+ major bump). |
Anyone can do it here and propose the change |
Lodash has a deep extend extension called extendify that has a sane implementation of array merge strategies. Might make sense to check that out for inspiration. |
Sounds like some folks already reached the same thought as I did in #32 (comment) - allowing consumers to pass in a custom array-merging function seems like a solid idea to me. |
And issue #24! |
I opened pull request #37 to solve this issue - comment there if you have any thoughts. |
Great library! I have a small issue with a certain behaviour though. Maybe it's on purpose.
I would expect
merged
to look like this:However it looks like this:
To me the behaviour I expect makes more sense because an array is likely to be a set of similar objects with shared keys but different values for those keys. However, I understand if that is not one of the behaviours that you would expect with the library so I shall make my own fork, and if you agree with my expected behaviour, I'll do a pull request for it 😄
The text was updated successfully, but these errors were encountered: