我正在创建收件箱,正在从API检索消息,我想将它们分组以创建收件箱,我的问题是我必须按2个值对它们进行分组。我必须按标题分组(消息与产品相关-产品标题),也必须按发件人分组(因此,只有在其他人发送邮件时,我才显示2条或更多...消息)
所以我的数组看起来像:
myMessagesSorted [
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 2,
"message": "This is just sample message 2",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 3,
"message": "This is just sample message 3",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 2",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 2",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 2,
"message": "This is just sample message 2",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 2",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 3",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 2,
"message": "This is just sample message 2",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 3",
},
]
我创建了一些对数组进行分组的函数:
var titles = [];
var uniquesData = [];
var index;
for (var i = 0; i < myMessagesSorted.length; i++) {
index = titles.indexOf(myMessagesSorted[i].title);
if (index == -1) {
titles.push(myMessagesSorted[i].title);
uniquesData.push(myMessagesSorted[i]);
}
}
myMessagesGrouped = uniquesData;
在这里我按标题(产品名称)对它们进行分组...问题是,对于数组中的“产品1”,我收到了来自两个不同人员(人员1和人员2)的消息...因此在这种情况下,我无法回复给两个不同的人。
如果我将by分组,那么这是相反的问题...对于相同的产品,我可以看到2条不同的消息,但是如果同一个人为其他产品发送了消息,则会被跳过...
var titles = [];
var uniquesData = [];
var index;
for (var i = 0; i < myMessagesSorted.length; i++) {
index = titles.indexOf(myMessagesSorted[i].from);
if (index == -1) {
titles.push(myMessagesSorted[i].from);
uniquesData.push(myMessagesSorted[i]);
}
}
myMessagesGrouped = uniquesData;
理想情况下,我需要像这样的分组数组:
array [
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 2",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 1",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 2",
},
Object {
"from": "Person 1",
"fromId": "1QzUz6IJxRhrfesIFmcy1zJ0aFD3",
"id": 1,
"message": "This is just sample message 1",
"postId": "-MCCl4Z-81uMIPGol-ab",
"read": 0,
"time": "Jul 29, 2020, 01:58 PM",
"title": "Product 3",
},
]
按产品名称分组,但是...仅在两个或两个以上的人询问同一产品时重复进行(这样,人们才能回复发送邮件的特定人)
我认为对我的小功能做很少的改动就可以欺骗...
谢谢!