如何合并两个JavaScript Object litterals

时间:2021-12-16 12:13:06

If I have the objects data1 and data2, how can I make data3?

如果我有对象data1和data2,我该如何创建data3?

I will send data3 as a parameter later in my AJAX request calling a MVC Server controller. (Just saying I can't have data3 to be an array I think).

我将稍后在调用MVC服务器控制器的AJAX请求中将data3作为参数发送。 (只是说我不能让data3成为一个数组我认为)。

var data1 = {
    managementPointId: 1,
    businessUnitId: 2
};

var data2 = {
    computerPackageId: 3
};

var data3 = {
    managementPointId: 1,
    businessUnitId: 2,
    computerPackageId: 3
};

3 个解决方案

#1


Use $.extend method:

使用$ .extend方法:

var data1 = {
    managementPointId: 1,
    businessUnitId: 2
};

var data2 = {
    computerPackageId: 3
};

var data3 = $.extend({}, data1, data2);

document.write('<pre>' + JSON.stringify(data3, null, 4) + '</pre>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#2


If you are interested to do in plain javascript,

如果你有兴趣用普通的javascript做,

simply declare a var like

只需声明一个var就好了

var obj3 = yourfunction(obj1,obj2,obj3);

var obj3 = yourfunction(obj1,obj2,obj3);

and in your function write a nested for loop which loops for arguments and loops through all props in objs provided and returns the obj.

并在您的函数中编写一个嵌套的for循环,循环参数并循环遍历所提供的objs中的所有道具并返回obj。

try it . take care about null checks and other corner cases like object checking etc...,

试试吧 。注意空检查和其他角落情况,如对象检查等...,

#3


Using plain JavaScript:

使用纯JavaScript:

function extend(a, b){
    for(var key in b)
        if(b.hasOwnProperty(key))
            a[key] = b[key];
    return a;
}


var data1 = {
    managementPointId: 1,
    businessUnitId: 2
};

var data2 = {
    computerPackageId: 3
};
var data3 = extend(data1, data2);


document.write('<pre>' + JSON.stringify(data3, null, 4) + '</pre>');
   

#1


Use $.extend method:

使用$ .extend方法:

var data1 = {
    managementPointId: 1,
    businessUnitId: 2
};

var data2 = {
    computerPackageId: 3
};

var data3 = $.extend({}, data1, data2);

document.write('<pre>' + JSON.stringify(data3, null, 4) + '</pre>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

#2


If you are interested to do in plain javascript,

如果你有兴趣用普通的javascript做,

simply declare a var like

只需声明一个var就好了

var obj3 = yourfunction(obj1,obj2,obj3);

var obj3 = yourfunction(obj1,obj2,obj3);

and in your function write a nested for loop which loops for arguments and loops through all props in objs provided and returns the obj.

并在您的函数中编写一个嵌套的for循环,循环参数并循环遍历所提供的objs中的所有道具并返回obj。

try it . take care about null checks and other corner cases like object checking etc...,

试试吧 。注意空检查和其他角落情况,如对象检查等...,

#3


Using plain JavaScript:

使用纯JavaScript:

function extend(a, b){
    for(var key in b)
        if(b.hasOwnProperty(key))
            a[key] = b[key];
    return a;
}


var data1 = {
    managementPointId: 1,
    businessUnitId: 2
};

var data2 = {
    computerPackageId: 3
};
var data3 = extend(data1, data2);


document.write('<pre>' + JSON.stringify(data3, null, 4) + '</pre>');