Open webketje opened 2 years ago
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:
import delve from 'dlv';
import { dset as dsetMergeOriginal } from 'dset/merge';
function dset(obj, keyPath, val, arrayMethod = 'push') {
let nestedValue = delve(obj, keyPath);
// manually handle merging of arrays ('dset/merge' will only merge objects)
if (Array.isArray(nestedValue) && Array.isArray(val)) {
for (let i = 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 secondarr
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".