如何$ .extend在jquery中包含子对象的对象?

时间:2021-12-28 07:33:04

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?

我知道我可以使用$ .extend({},x);将两个或多个对象的内容合并到第一个对象中,但我的答案是,和子对象?他们只会被覆盖?我不能合并呢?

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:

这个jQuery函数会返回这个:

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 个解决方案

#1


6  

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

对于深度嵌套尝试,$ .extend(true,{},x);.对于深度嵌套,格式为:

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

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

#1


6  

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

对于深度嵌套尝试,$ .extend(true,{},x);.对于深度嵌套,格式为:

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

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