为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕标5832272 2022-01-07 21:10:22
我有一个这样的对象数组: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"}];怎么做?
查看完整描述

3 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

您可以为此目的使用 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) {

    const fields = timeStr.split(":").map(parseInt);

    return fields[0] * 60 + fields[1];

}


let result = messages.reduce( (map, item) => { 

   if (!map[item.sender] || parseTime(map[item.sender].time) < parseTime(item.time)) {

       map[item.sender] = item;

   } 

   return map;

}, {});


console.log("Latest messages:", Object.values(result));


查看完整回答
反对 回复 2022-01-07
?
holdtom

TA贡献1805条经验 获得超10个赞

你可以这样做: -


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) => {

    const msgMap = {};

    const uniqueMsg = [];

    messages.forEach(item => {

        if(!msgMap[item.sender]) {

            msgMap[item.sender] = true;

            uniqueMsg.push(item)

        }

    });


   return uniqueMsg;   

}


// console.log(getUniqueMessages(messages));


查看完整回答
反对 回复 2022-01-07
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

const result = [];

const map = new Map();

for (const item of messages) {

    if(!map.has(item.sender)){

        map.set(item.sender, true);

        result.push({

            message: item.message,

    reciever: item.reciever

            sender: item.sender,

            time: item.time

        });

    }

}

console.log(result)


查看完整回答
反对 回复 2022-01-07
  • 3 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信