[英]I have an array of objects and want to combine them into one object IF the one of the keys match (without overwriting the initial properties)
[英]i have two array of objects, i want to update the one which have same messageId and keep the unique one in one array in react javascript
/*這是我的第一個包含兩個對象的數組。 我想要的是我想比較兩個 arrays 的 object 並檢查是否有任何味精具有相同的 ID,如果有任何味精具有相同的 ID 我想從第二個數組更新相同的 ID 味精並將不相同的味精保存在數組中 * /
const messageListState = [
{
"profileId": "",
"receiverId": "1072e315-a372-4f3f-81c0-344979980add",
"messageId": "e2397e7e-9509-46ae-b53e-4af8008bdca6",
"message": "Thank you for connecting to the bykea.",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "",
"msgDate": 1645696234953,
"sentDate": 1645696234953,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "0A3F7E50-60B5-466F-AA2E-33BA32B26A2B",
"message": "You are now chatting with Azaan Khan",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
}]
/這是我的 function /
const updateById = (obj) => {
return obj.map((msg)=>{
return messageListState.map((message) =>
{return message?.messageId === msg.messageId ? msg : message})
})
}
/這里我用我的第二個數組作為參數調用 function /
const newMsg = updateById(
[ {
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "0A3F7E50-60B5-466F-AA2E-33BA32B26A2B",
"message": "iloveu",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": true,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "1",
"message": "mohti",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
}
]
)
console.log(newMsg)
/我想要的結果是這樣的: /
newMsg = [
{
"profileId": "",
"receiverId": "1072e315-a372-4f3f-81c0-344979980add",
"messageId": "e2397e7e-9509-46ae-b53e-4af8008bdca6",
"message": "Thank you for connecting to the bykea.",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "",
"msgDate": 1645696234953,
"sentDate": 1645696234953,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "0A3F7E50-60B5-466F-AA2E-33BA32B26A2B",
"message": "iloveu",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": true,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "1",
"message": "mohti",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
}
]
const messageListState = [
{
"profileId": "",
"receiverId": "1072e315-a372-4f3f-81c0-344979980add",
"messageId": "e2397e7e-9509-46ae-b53e-4af8008bdca6",
"message": "Thank you for connecting to the bykea.",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "",
"msgDate": 1645696234953,
"sentDate": 1645696234953,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "0A3F7E50-60B5-466F-AA2E-33BA32B26A2B",
"message": "You are now chatting with Azaan Khan",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
}];
const state2= [ {
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "0A3F7E50-60B5-466F-AA2E-33BA32B26A2B",
"message": "iloveu",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": true,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
},
{
"profileId": "1072e315-a372-4f3f-81c0-344979980add",
"receiverId": "agent001@gmail.com",
"messageId": "1",
"message": "mohti",
"msgType": "text",
"viewObject": "",
"isDeleted": false,
"isEdited": false,
"replyMsgId": "0",
"msgDate": 1645696247563,
"sentDate": 1645696247563,
"caption": "",
"thumbnail": "",
"customData": "",
"chatId": "235308",
"departmentid": "37",
"departmentName": "Foreign Remittance",
"viewId": "0",
"viewDet": null,
"viewOptionId": null,
"originalFileName": "",
"msgHeading": "",
"msgURL": "",
"dType": "",
"departmentName_u": "",
"dImage": "",
"audioLength": "",
"buttonDet": null,
"tempProfileId": null,
"campaignId": "",
"msgExpTime": 0
}
]
const compareArr=(arr1,arr2)=>{
let items1=arr1.map((item)=>{
let i=arr2.findIndex((it)=>it.messageId===item.messageId);
if(i!=-1){
item.message=arr2[i].message;
}
return item;
});
let items2=arr2.filter((i)=>!items1.find((it)=>it.messageId===i.messageId));
return [...items1,...items2];
}
console.log(compareArr(messageListState,state2));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.