3

I know i can use $.extend({}, x); to merge the contents of two or more objects together into the first object, but my answer is, and the sub-objects? They will only be overwritten? I can't merge then?

I have this:

var settings = $.extend({
    modal: {
        title: "Atention",
        type: "text",
        width: "small"
    },
    ajax: {
        type: "normal",
        trigger: "",
        url: ""
    }
}, {
    modal: {
        title: "Registrar",
        type: "register"
    },
    ajax: {
        trigger: ".bumbum",
        url: "modal/lets/go"
    }
});

This jQuery function will return this:

var setting = {
    modal: {
        title: "Registrar",
        type: "register"
    },
    ajax: {
        trigger: ".bumbum",
        url: "modal/lets/go"
    }
};

And unfortunately I need it:

var setting = {
    ajax: {
        type: "normal",
        trigger: ".bumbum",
        url: "modal/lets/go"
    },
    modal: {
        title: "Registrar",
        type: "register",
        width: "small"
    }
};

Any idea? Thank you.

1 Answer 1

6

For deep nesting try, $.extend(true, {}, x);. For deep nesting the format is:

jQuery.extend( [ deep ], target, object1 [, objectN ] );

Sign up to request clarification or add additional context in comments.

2 Comments

There. I thought you wouldn't move the true. :D
Man, so simple... I just look into the first function into the docs jQuery.extend( target [, object1 ] [, objectN ] ), a new lesson has been learned. Read the docs until the end... haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.