简体   繁体   中英

Ramda does not provide deep mixin?

var _ = require('ramda');

var obj1 = {
    innerNum: 1,
    innerObj: {
        innerStr: 'a',
        innerStrB: 'Z'
    }
};

var obj2 = {
    innerNum: 2,
    innerObj: {
        innerStr: 'b'
    }
};

var mixedObj = _.mixin(obj1, obj2);

mixedIObj does not include the inner object's innerStrB. Any ramda solution?

It's not clear what you would want here. Most mixin / extend implementations I've seen are shallow, adding the values of the keys in the second object to those of the first, overriding where the keys are duplicated.

Ramda does have functions to clone an object with an updated value at a particular path: assocPath . I'm guessing that this wouldn't do everything you seem to want, though:

R.assocPath('innerObj.innerStr', 'b', obj1); //=> 
// { 
//    innerNum: 1,
//    innerObj: {
//      innerStr: 'b',
//      innerStrB: 'Z'
//    }
// }

The question is what a deep mixin would really mean. If the objects have the same structure, it's pretty clear, but if they don't, it might be a little hairy:

mixinDeep(
    {a: 1, b: {c: 2, d: 3}},
    {b: {d: {e: 4, f: 5}, g: 6}}
);
// ?=>? {a: 1, b: {c: 2, d: {e: 4, f: 5}, g: 6}}

If that's the sort of thing you're looking for, then the Ramda team would love to have an issue raised or even a pull request .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM