[英]Merge multiple objects into single object with making array of same property values
I have five json objects obj1 ,obj2,obj3,obj4 and obj5, i want to merge them and crete a single json object and obj have same columns.我有五个 json 对象 obj1 、obj2、obj3、obj4 和 obj5,我想合并它们并生成一个 json 对象并且 obj 具有相同的列。 The resultant json should have all the values from all objects
生成的 json 应该包含所有对象的所有值
Question:题:
var obj1 = {
"DOP":"manu",
"Resolution":23,
"sensor":"SE"
}
var obj2 = {
"DOP":"mansa",
"Resolution":22,
"sensor":"PS"
}
like that all five object data and results should update only rows not as columns and expected result should be就像所有五个对象数据和结果应该只更新行而不是列,预期结果应该是
var result = {
"Dop":["manu","mansa"],
"Resolution":[23,25],
"Sensor":["SE","PS"]
}
You can use for(x in obj)
您可以使用
for(x in obj)
var result = {}
for (x in obj1) { result[x] = obj1[x] + ', ' + obj2[x] }
//Object {DOP: "manu, mansa", Resolution: "23, 22", sensor: "SE, PS"}
You can merge object based on passing unlimited objects as arguments in myCustomMerge()
function您可以基于在
myCustomMerge()
函数中传递无限对象作为参数来合并对象
function myCustomMerge() { var return_obj = {}; for (i in arguments) { obj = arguments[i]; for (j in obj) { if (return_obj[j] == undefined) { return_obj[j] = [obj[j]]; } else { return_obj[j].push(obj[j]); } } } return return_obj; } var obj1 = { "DOP": "manu", "Resolution": 23, "sensor": "SE" } var obj2 = { "DOP": "mansa", "Resolution": 22, "sensor": "PS", "special_obj2": "My Special Value" } var obj3 = { "DOP": "top", "Resolution": 36, "sensor": "ME", "special_obj2": "My Special Value 2" } var merged_obj = myCustomMerge(obj1, obj2, obj3); console.log(merged_obj);
The way you want result is illegal.你想要结果的方式是非法的。 All you can have array of your json.
所有你可以拥有你的json数组。
var result = [{ "DOP":"mansa", "Resolution":22, "sensor":"PS" }, { "DOP":"manu", "Resolution":23, "sensor":"SE" } ]
var x ={ "student-marks":{'math':1,'physics':5} }; var y ={ "student-marks":{'chemistry':3,'history':2} }; $.extend(true, {}, x, y);
我从这里得到了答案: 如何通过键合并两个对象值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.