如何获取集合中每个发件人的最新消息?

我有一个这样的对象数组:


const messages = [ 

  {message: "ghhhhhhhh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "12:56"},

  {message: "ggggggghjjgcgh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:45"},

  {message: "good afternoon", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:41"},

  {message: "hfdsghfdfhjo", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:38"},

  {message: "hhhhhhhhhhhhh ", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "11:50"}

];

我想获取每个发件人的最新消息,如下所示:


const messages = [

  {message: "ghhhhhhhh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "12:56"},

  {message: "ggggggghjjgcgh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:45"}

];

怎么做?


慕标5832272
浏览 115回答 3
3回答

心有法竹

您可以为此目的使用 array.reduce,例如:const messages = [ {message: "ghhhhhhhh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "12:56"}, {message: "ggggggghjjgcgh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:45"}, {message: "good afternoon", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:41"}, {message: "hfdsghfdfhjo", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:38"}, {message: "hhhhhhhhhhhhh ", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "11:50"}];function parseTime(timeStr) {&nbsp; &nbsp; const fields = timeStr.split(":").map(parseInt);&nbsp; &nbsp; return fields[0] * 60 + fields[1];}let result = messages.reduce( (map, item) => {&nbsp;&nbsp; &nbsp;if (!map[item.sender] || parseTime(map[item.sender].time) < parseTime(item.time)) {&nbsp; &nbsp; &nbsp; &nbsp;map[item.sender] = item;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;return map;}, {});console.log("Latest messages:", Object.values(result));

holdtom

你可以这样做: -const messages = [ {message: "ghhhhhhhh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "12:56"}, {message: "ggggggghjjgcgh", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:45"}, {message: "good afternoon", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:41"}, {message: "hfdsghfdfhjo", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "ZCiuWczin3VuibH59MISuEqR3pc2", time: "12:38"}, {message: "hhhhhhhhhhhhh ", receiver: "OX0pReHXfXUTq1XnOnTSX7moiGp2", sender: "14", time: "11:50"}]const getUniqueMessages = (messages) => {&nbsp; &nbsp; const msgMap = {};&nbsp; &nbsp; const uniqueMsg = [];&nbsp; &nbsp; messages.forEach(item => {&nbsp; &nbsp; &nbsp; &nbsp; if(!msgMap[item.sender]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msgMap[item.sender] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uniqueMsg.push(item)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp;return uniqueMsg;&nbsp; &nbsp;}// console.log(getUniqueMessages(messages));

慕丝7291255

const result = [];const map = new Map();for (const item of messages) {&nbsp; &nbsp; if(!map.has(item.sender)){&nbsp; &nbsp; &nbsp; &nbsp; map.set(item.sender, true);&nbsp; &nbsp; &nbsp; &nbsp; result.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: item.message,&nbsp; &nbsp; reciever: item.reciever&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sender: item.sender,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time: item.time&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}console.log(result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript