简体   繁体   English

如何 map 按所需 object 值分组的嵌套 arrays 数组

[英]How to map an array of nested arrays grouped by desired object values

I have an array of messages which are like the following:我有一组消息,如下所示:

[
  { message: 'This is message', to: 4, from: 1},
  { message: 'This is response message', to: 1, from: 4 },
  { message: 'This is ANOTHER message with different sender', to: 1, from: 2 },
  { message: 'This is RESPONSE message to different sender', to: 2, from: 1 },
]

I have all the messages, but they are not grouped by the user-to-user conversation.我有所有的消息,但它们没有按用户对用户的对话分组。 What I want is to group the messages (inside an array) by to and from .我想要的是通过tofrom对消息(在数组内)进行分组。

How can I achieve the following outcome:我怎样才能达到以下结果:

[
  [
    { message: 'This is message', to: 4, from: 1},
    { message: 'This is response message', to: 1, from: 4 },
  ],
  [
    { message: 'This is ANOTHER message with different sender', to: 1, from: 2 },
    { message: 'This is RESPONSE message to different sender', to: 2, from: 1 }
  ],
]

In short:简而言之:

Current structure is目前的结构是

// Individually structured messages
[message, message, message, message]

Desired outcome:期望的结果:

// Grouped by user-to-user conversation
// [[Conversation1], [Conversation2]]
[[message, message], [message,message]]

I have tried using lodash with return this._.values(this._.groupBy(this.messages, 'to')) , but since I need to group them by two criteria, I didn't manage to come up with the logic.我曾尝试将 lodash 与return this._.values(this._.groupBy(this.messages, 'to')) ,但由于我需要按两个标准对它们进行分组,所以我没能想出逻辑。

Javascript implementation with Array.reduce and Array.find Javascript 使用Array.reduceArray.find实现

 const messages = [ { message: 'This is message', to: 4, from: 1}, { message: 'This is response message', to: 1, from: 4 }, { message: 'This is ANOTHER message with different sender', to: 1, from: 2 }, { message: 'This is RESPONSE message to different sender', to: 2, from: 1 }, ]; const groupedMessage = messages.reduce((acc, curr) => { const node = acc.find(item => item.find((msg) => (msg.from === curr.to && msg.to === curr.from) || (msg.from === curr.from && msg.to === curr.to))); node? node.push(curr): acc.push([curr]); return acc; }, []); console.log(groupedMessage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按键分组的对象数组的总和值并返回到新的数组数组中 - Sum values of array of object grouped by keys and return into a new array of arrays 如何在Javascript中将数组值映射到对象内部的数组? - How to map array values to arrays inside an object in Javascript? 如何在本机反应中嵌套 arrays 的 map object - How to map object of nested arrays in react native 在javascript中映射嵌套数组中的非对象值 - map non-object values in nested arrays in javascript 将对象数组转换为按值分组的数组数组 - Convert array of objects to array of arrays grouped by values 如何使用嵌套数组对对象中的值求和? - How to sum values in object with nested arrays? 将数组对象转换为所需格式的对象数组 - converting object of arrays to an array of objects in a desired format 如何从嵌套在 object 内的数组中的 object 中提取值并将其存储在不同的 arrays Z53C6C951F4E558B9DFF298869 - how to extract values from an object nested inside array inside object and store it in different arrays JAVASCRIPT 如何对 object 中的值进行分组? - How to grouped values in object? 如何用嵌套的 arrays 将数组表示为 object - How to represent array with nested arrays as an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM