我想使用JavaScript合并两个对象
第一
{
"user" : " Hari",
"friend" : "Shiva",
"friendList": ["Hanks"," Tom"," Karma"," Hari"," Dinesh"]
}
第二
{
"user" : "Hari",
"friend" : " Shiva",
"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva",
" Kishna"," Bikash"," Bakshi"," Dinesh"]
}
并形成一个对象:
预期输出
{
"user" : "Hari"
"friend" : "Shiva",
"friendList":[
["Hanks"," Tom","Karma"," Hari"," Dinesh"],
["Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]
]
}
有可能吗?如果这是错误的问题,我很抱歉....但我需要以这种方式解决,我对JavaScript没有太多了解。
答案 0 :(得分:0)
您可以从一个数组添加到像这样的
var a={
"user" : " Hari",
"friend" : "Shiva",
"friendList": ["Hanks"," Tom"," Karma"," Hari"," Dinesh"]
};
var b={
"user" : "Hari",
"friend" : " Shiva",
"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva",
" Kishna"," Bikash"," Bakshi"," Dinesh"]
};
b.friendList.concat(a.friendList);
你将获得a + b的所有数组......
你不会得到你想要的这种结构。对于我的代码,你将从a和b获得一个字符串数组,如果你想要这个结构,你需要将结果更改为一个对象而不是数组。
也可以看到以下示例:link
答案 1 :(得分:0)
以下代码将合并您问题中描述的对象:
var firstObject = {
"user" : " Hari",
"friend" : "Shiva",
"friendList": ["Hanks"," Tom"," Karma"," Hari"," Dinesh"]
};
var secondObject = {
"user" : "Hari",
"friend" : " Shiva",
"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva",
" Kishna"," Bikash"," Bakshi"," Dinesh"],
};
function mergeMyObjectsIntoNew(first,second,copyFriendListArrayReference){
//.trim() is needed in the compare here because example data has a leading space
// in secondObject.friend
if(first.user.trim() === second.user.trim()
&& first.friend.trim() === second.friend.trim()){
var newObject={}; //The new object
newObject.user = first.user;
newObject.friend = first.friend;
newObject.friendList = []; //Create new array
if(copyFriendListArrayReference){
//This copies references for the arrays in first.friendList and
// second.friendList into the array new.friendList. These will be the
// same arrays as exists in first and second. Changes to the contents
// of those arrays will affect the arrays in the newObject.
newObject.friendList[0] = first.friendList; //add friendList from first
newObject.friendList[1] = second.friendList; //add friendList from second
}else{
//This copies the string contents of the arrays in first.friendList and
// second.friendList into the array new.friendList. These will NOT be the
// same arrays as exists in first and second. Changes to the contents
// of those arrays will NOT affect the arrays in the newObject.
// This is the default behavior.
//Copy contents of friendList from first
newObject.friendList[0] = Array.from(first.friendList);
//Copy contents of friendList from second
newObject.friendList[1] = Array.from(second.friendList);
}
return newObject;
}//implicit else due to 'return' in all if paths
return null; //Indicate to caller that there was not a match.
}
//Get new merged object with copy of friendList
mergedObject = mergeMyObjectsIntoNew(firstObject,secondObject);
console.log(mergedObject);

使用多个非常相似的结构化对象通常是一个坏主意
通常,使用具有两种不同结构的对象并不是一个好主意,除非您将这些对象明确地分开。如果您对对象进行了多次类似处理(即使用相同的函数/方法来操作具有两种结构的对象),则所有代码都必须考虑多个结构中的每一个。这可能会使您的代码变得更加复杂,并且如果您没有多个类似于对待的对象的结构,则可能会更加混乱。
在这种情况下,您希望结果对象具有friendList
属性,该属性是字符串数组的数组,而源对象具有friendList
属性,该属性是字符串数组。如果您要在两个对象结构上使用mergeMyObjectsIntoNew
,那么friendList
属性的两个不同结构将需要mergeMyObjectsIntoNew
中的更多逻辑,如果您希望它能够在mergedFirstSecondObject = mergeMyObjectsIntoNew(firstObject,secondObject);
mergedthirdFourthObject = mergeMyObjectsIntoNew(firstObject,secondObject);
mergedFourObjects = mergeMyObjectsIntoNew(mergedFirstSecondObject,mergedthirdFourthObject);
上运行这两种类型的对象都有你可能期望的结果。
例如,以下代码不会产生您可能期望的结果:
friendList
这将生成一个具有mergedFourObjects
属性的对象,该属性是一组字符串数组。 {
"user" : " Hari",
"friend" : "Shiva",
"friendList": [
[
friendListArrayFromFirstObject,
friendListArrayFromSecondObject
],
[
friendListArrayFromThirdObject,
friendListArrayFromFourthObject
]
]
}
对象的结构如下:
friendList
在你的情况下,你可能最好选择总是让friendList
属性只是一个字符串数组或一个字符串数组中的一个或另一个。拥有MyObject.prototype.compare(second)
属性使其可能是任何一种结构都会使代码在使用这些对象时大大复杂化。
使用类是处理对象的更好方法
如果您要在更大的项目中使用对象,那么为对象使用类可能更好。这将允许您移动诸如比较对象和将对象合并到类的方法(例如MyObject.prototype.merge(second)
和cf ic logs -f CONTAINER_NAME
之类的东西。但是,这是一个超出您的问题范围的讨论。如果您想要了解更多信息,我建议您搜索有关在JavaScript中使用对象类的信息。