简体   繁体   English

使用相同属性值的数组将多个对象合并为单个对象

[英]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.

相关问题 如果对象的属性值在两个对象数组中相等,则将这些对象合并为单个对象 - If property values of objects are equal in two array of objects then merge those objects into single object 比较数组中的对象,合并具有相同属性值的重复项,将属性值添加到合并的对象中 - Compare objects in array, merge duplicates with same property value, add property values to the merged object 使用JavaScript中的相同键将多个json对象合并为单个对象 - Merge multiple json objects to single object with same keys in javascript 将对象数组合并到单个 object - Merge array of objects into single object 将具有相同属性的对象数组转换为具有数组值的一个对象 - Convert array of objects with same property to one object with array values 合并一个属性相同的对象数组,同时将另一个属性的唯一值转移到一个数组中 - merge an array of objects where one property is the same, while transferring the unique values of another property into an array 如何将具有相同属性的对象合并到一个数组中? - How to merge objects with the same properties into a single Array? 对象数组的对象。 获取具有相同属性的所有值的长度 - Object of array of objects. Get the length of all values with the same property 将两个数组合并为具有属性值的对象数组 - Merge two arrays into an array of objects with property values 对象数组到属性值的对象 - Array of objects to object of property values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM