简体   繁体   English

我有两个对象数组,我想更新具有相同 messageId 的对象,并在反应 javascript 中保留唯一的一个数组

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

/*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 */我想要的是我想比较两个 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
    }]

/ here is my function / /这是我的 function /

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

/ here im calling the function with my second array as a parameter / /这里我用我的第二个数组作为参数调用 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)

/ 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": "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.

相关问题 我有一组对象,如果其中一个键匹配(不覆盖初始属性),我想将它们组合成一个 object - 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) 如果有匹配的元素,如何比较 2 个单独的对象/数组是否有一个相同的元素并将 2 个对象连接到一个数组中? Javascript - How to compare if, 2 separate Objects/Array have one same element and concat 2 objects into one array, if there is matching element? Javascript 我有六个反应文件,每个文件都导出一个数组,我想将六个文件减少到一个 - I have six react files each exports an array and I want to reduce the six files to one 我有两个约会。 我想通过时间差设置另一个与第一个相同的 - I have two dates. i want to set another one same as first one by taking time difference 我需要比较两个 arrays 并让 output 成为一个数组,在里面比较对象 - I need to compare two arrays and have the output be one array, objects being compared inside jQuery比较两个数组对象以拥有一个最终的数组对象 - jQuery compare two arrays objects to have one final array objects 如果我的javascript数组的值包含两个元素,如何使用一个元素而不使用另一个元素? - If my javascript array has values that have two elements how can I use one element without the other? 我有两个数组,一个有 N 个数据,另一个有固定的数据 - I have two array, one have N no of data and and another have fixed no of data 如果数组中的对象具有相同的值,则将它们合并为一个对象 - If objects into an array have the same value merge them in one object 我可以使用React和Javascript将这两个数组组合成一个对象吗? - Can I combine these two array into one object using React and Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM