I am creating inbox, I am retrieving messages from API and I want to group them to create inbox, my problem is I have to group them by 2 values. I have to group them by title (messages are related to products - product title), and also I have to group them by sender ( so I display 2 or more... messages only in case different people sent them)
so my Array look something like:
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",
},
]
I created little function to group array:
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;
Here I group them by title (product name)... Problem is that for "Product 1" from array I received messages from two different people (Person 1 and Person 2)... so in this case I cannot reply to two different people.
If I group the by from... then it is opposite problem... I see 2 different messages for the same product fine, but if the same person sent message for some other product it is skipped there... :
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;
Ideally I need grouped array like:
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",
},
]
Grouped by product name, but... only repeated if two or more people enquired about the same product (so people can respond to particular person who sent message)
I assume little change to my little function can do trick...
Thanks!