-
Notifications
You must be signed in to change notification settings - Fork 217
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
Partial array merge #190
Comments
Yup, a custom |
Thanks, the arrayMerge doesn't seems to fit well because is applied to all the arrays, but implementing a deeper logic to the customMerge seems to work. I've tried something like this, but if you have some reviews to the snippet I would appreciate. const x = {
foo: { bar: 3 },
array: [
{
does: 'work',
too: [ 1, 2, 3 ]
},
{
does: 'yep',
too: [ 1, 1, 1 ]
}]
}
const y = {
foo: { baz: 4 },
quux: 5,
array: {
'__deepmerge_index': 0,
does: 'replaced',
too: [ 4, 5, 6 ]
}
}
const deepmerge_index_key = '__deepmerge_index';
const mergeOnIndex = (obj1, obj2) => {
if (Array.isArray(obj1) && typeof obj2 === 'object') {
if (obj2.hasOwnProperty(deepmerge_index_key)) {
var indexToChange = obj2[deepmerge_index_key];
delete obj2[deepmerge_index_key];
obj1[indexToChange] = obj2;
return obj1;
}
}
return merge(obj1, obj2)
}
const merged = merge(x, y, {
customMerge: (key) => mergeOnIndex
});
const output = {
foo: {
bar: 3,
baz: 4
},
array: [
{
does: 'replaced',
too: [ 4,5,6 ]
},
{
does: 'yep',
too: [ 1, 1, 1 ]
}]
} |
As far as I understood there is no way to handle a merge of a specific item in an array, is that correct?
I would like to update only the object in a specific index leaving the others untouched.
Should I handle this with a custom merge function?
The text was updated successfully, but these errors were encountered: