You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would expect the result to be { arr: ['hello', 'world'] }, however currently the result is { arr: ['world'] }
If I change the second arr to barr the result is { arr: ['hello'], barr: ['world'] }
The README says:
The main/default dset module forcibly writes values at the assigned key-path. However, in some cases, you may prefer to merge values at the key-path
So I would expect arrays to merge too instead of being forcibly overwritten as in the 'standard' dset
Anyhow i understand the merge logic is similar to lodash.merge.
The deepmerge module has a merge strategy option, but it only handles merging, not what dset does.
It would be great if dset could be used with a strategy of "add" instead of "overwrite".
This was also my expectation about dset/merge when dealing with arrays. But thinking a bit about this, it's not so clear what is meant by "merging" in the context of arrays. Usually the new elements should be placed at the end, but sometimes might be at the beginning.
Anyway, it's easy to create a wrapper around dset/merge to handle this case:
importdelvefrom'dlv';import{dsetasdsetMergeOriginal}from'dset/merge';functiondset(obj,keyPath,val,arrayMethod='push'){letnestedValue=delve(obj,keyPath);// manually handle merging of arrays ('dset/merge' will only merge objects)if(Array.isArray(nestedValue)&&Array.isArray(val)){for(leti=0;i<val.length;i++){nestedValue[arrayMethod](val[i]);}}else{dsetMergeOriginal(obj,keyPath,val);}}export{dset}
Given following code:
I would expect the result to be
{ arr: ['hello', 'world'] }
, however currently the result is{ arr: ['world'] }
If I change the second
arr
tobarr
the result is{ arr: ['hello'], barr: ['world'] }
The README says:
So I would expect arrays to merge too instead of being forcibly overwritten as in the 'standard'
dset
Anyhow i understand the merge logic is similar to lodash.merge.
The
deepmerge module
has a merge strategy option, but it only handles merging, not whatdset
does.It would be great if dset could be used with a strategy of "add" instead of "overwrite".
The text was updated successfully, but these errors were encountered: