Home > Software engineering >  i have two array of objects, i want to update the one which have same messageId and keep the unique
i have two array of objects, i want to update the one which have same messageId and keep the unique

Time:02-25

/*here its my first array which contains two objects. what i want is i want to compare two arrays of object and check if any msg have the same id, if any msg have the same id i want update the same id msg from the second array and keep the non same msg in an array */

   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": "[email protected]",
    "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
    }]

/here is my function/

   const updateById = (obj) => {
   return  obj.map((msg)=>{
   return messageListState.map((message) => 
   {return message?.messageId === msg.messageId ? msg : message})
   })
  }

/here i m calling the function with my second array as a parameter/

  const newMsg = updateById(
    [ {
    "profileId": "1072e315-a372-4f3f-81c0-344979980add",
    "receiverId": "[email protected]",
    "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": "[email protected]",
  "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)

/the result i want is like this:/

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": "[email protected]",
    "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": "[email protected]",
  "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
     }
]

CodePudding user response:

  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": "[email protected]",
"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": "[email protected]",
  "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": "[email protected]",
"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));
  • Related