如何将邮件中的文本分组?

时间:2019-07-05 10:43:18

标签: javascript arrays vue.js lodash javascript-objects

美好的一天! 我正在聊天,并且hat中有许多消息。

[{"id":4,
"user_id":1,
"messageable_id":3,
"messageable_type":"conversation",
"text":"Text 1",
"action":null,
"target_id":null,
"created_at":"2019-06-17 15:47:55",
"updated_at":"2019-06-17 15:47:55",
"replies":[],
"files":[]},

{"id":5,
"user_id":1,
"messageable_id":3,
"messageable_type":"conversation",
"text":"Text 2",
"action":null,
"target_id":null,
"created_at":"2019-06-17 15:48:00",
"updated_at":"2019-06-17 15:48:00",
"replies":[],
"files":[]}]

如果时间差小于60秒,我想在一条消息中合并消息文本

[{"id":4,
"user_id":1,
"messageable_id":3,
"messageable_type":"conversation",
"grouped_text":["Text 1","Text 2"], //GROUP 
"action":null,
"target_id":null,
"created_at":"2019-06-17 15:47:55",
"updated_at":"2019-06-17 15:47:55",
"replies":[],
"files":[]}]

我尝试这段代码

let messages = this.$store.getters['conversation/messages'](this.conversation);
let grouped = _.groupBy(messages, message => {
    return this.$moment(message.created_at).startOf('minute');
});

我该如何做得更好?我可以用lodash

提前谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用reduce。如果前一条消息的created_at与当前消息的created_at之差小于1分钟(或60,000ms),则将该消息推到累加器中最后一条消息的grouped_text。否则,使用grouped_text属性将新消息推送到累加器

您可以destructure每条消息分别获得textrest的属性

const messages=[{"id":4,"user_id":1,"messageable_id":3,"messageable_type":"conversation","text":"Text 1","action":null,"target_id":null,"created_at":"2019-06-17 15:47:55","updated_at":"2019-06-17 15:47:55","replies":[],"files":[]},{"id":5,"user_id":1,"messageable_id":3,"messageable_type":"conversation","text":"Text 2","action":null,"target_id":null,"created_at":"2019-06-17 15:48:00","updated_at":"2019-06-17 15:48:00","replies":[],"files":[]}]

const groupDates = (date1, date2) => (new Date(date1) - new Date(date2) < 60000)

const grouped = messages.reduce((r, { text, ...rest }, i, arr) => {
  const prev = arr[i - 1];

  if (prev && groupDates(rest.created_at, prev.created_at))
    r[r.length - 1].grouped_text.push(text)
  else
    r.push({ ...rest, grouped_text: [text] })

  return r;
}, [])

console.log(grouped)

注意:这假设消息是根据created_at

排序的

答案 1 :(得分:0)

lodash的groupBy功能不是正确的工具。 groupBy函数会将所有与键匹配的消息对象(在您的情况下,同一分钟内的所有消息)都放入一个数组中。

您可以使用reduce功能。假设messages(问题中提到的消息)按日期排序:

const messages=[{"id":4,"user_id":1,"messageable_id":3,"messageable_type":"conversation","text":"Text 1","action":null,"target_id":null,"created_at":"2019-06-17 15:47:55","updated_at":"2019-06-17 15:47:55","replies":[],"files":[]},{"id":5,"user_id":1,"messageable_id":3,"messageable_type":"conversation","text":"Text 2","action":null,"target_id":null,"created_at":"2019-06-17 15:48:00","updated_at":"2019-06-17 15:48:00","replies":[],"files":[]}];

const result = messages.reduce((result, message) => {
    const lastMessage = result[result.length - 1];
    if (!lastMessage || new Date(message.created_at) - new Date(lastMessage.created_at) >= 60000) {
        message.grouped_text = [message.text]
        delete message.text;
        result.push(message);
    } else {
        lastMessage.grouped_text.push(message.text);
    }
    return result;
}, []);

console.log(result);