Skip to content
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

Vue.merge, reactive deep merge of objects #9853

Closed
wendt88 opened this Issue Apr 10, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@wendt88
Copy link

wendt88 commented Apr 10, 2019

What problem does this feature solve?

I want to merge two or more objects like _.merge, $.extend or Object.assign, but I can't get the changes outside the component, becoause this methods does not call the Vue.set.
I created a custom method like below, but it would be nice to have this feature out of the box

What does the proposed API look like?

Vue.prototype.$merge = (baseValue, value) => {
    // merge arrays
    if (
        Array.isArray(baseValue)
        && Array.isArray(value)
    ) {
        for (let i = 0; i < value.length; i  ) {
            let val = Vue.prototype.$merge(baseValue[i], value[i]);
            if (val === undefined)
                continue;
            if (baseValue[i])
                Vue.set(baseValue, i, val);
            else
                baseValue.push(val);
        }
    }
    // merge objects
    else if (
        value
        && baseValue
        && typeof value === 'object'
        && typeof object === 'object'
    ) {
        for (let key of Object.keys(value)) {
            let val = Vue.prototype.$merge(baseValue[key], value[key]);
            if (val === undefined)
                continue;
            Vue.set(baseValue, key, val);
        }
    }
    // return value
    else if (value !== undefined)
        return value;
    else
        return baseValue;
};
@yyx990803

This comment has been minimized.

Copy link
Member

yyx990803 commented Apr 10, 2019

This can be done in userland as a custom helper - I think the use case is quite niche to be included in core. As stated in the past, we intend to avoid adding pure utility functions unless it cannot be easily implemented by the user.

@yyx990803 yyx990803 closed this Apr 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.