Reputation: 167
I got an object which contains some values.
How do i transform this properly?
I tried multiple things like includes(), find(), some(), for-loops, but it seems like the check if 'conversationWith' is included in the object is failing.
The object looks like this:
{
_id: 5d3eed4b8558ab0fc513a3b5,
subject: 'Subject',
message: 'Message',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
{
_id: 5d3eed608558ab0fc513a3b7,
subject: '2nd',
message: '2nd',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
{
_id: 5d3ef15e6a570c1201457918,
subject: '3rd',
message: '3rd',
newMessage: true,
from: 5d3b0585181c521610a15241,
fromName: 'John Doe',
to: 5d3b0749c9b633171fa62a48,
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}
I want to create an array like this out of it:
[{
conversationWith: "Leeroy Jenkins",
message: ["Message", "2nd", "3rd"]
}]
The Problem is that i have multiple objects that I'm looping through, where I only want the 'conversationWith' property to be inserted into the array once, while i want all the messages in it.
I either get the 'conversationWith' property inserted multiple times or not at all.
Upvotes: 1
Views: 100
Reputation: 12152
You can use a simple forEach
loop
var a= [{
_id: '5d3eed4b8558ab0fc513a3b5',
subject: 'Subject',
message: 'Message',
newMessage: true,
from: '5d3b0585181c521610a15241',
fromName: 'John Doe',
to: '5d3b0749c9b633171fa62a48',
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
},
{
_id: '5d3eed608558ab0fc513a3b7',
subject: '2nd',
message: '2nd',
newMessage: true,
from: '5d3b0585181c521610a15241',
fromName: 'John Doe',
to: '5d3b0749c9b633171fa62a48',
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
},
{
_id: '5d3ef15e6a570c1201457918',
subject: '3rd',
message: '3rd',
newMessage: true,
from: '5d3b0585181c521610a15241',
fromName: 'John Doe',
to: '5d3b0749c9b633171fa62a48',
toName: 'Leeroy Jenkins',
conversationWith: 'Leeroy Jenkins'
}]
var b=[];
var bool=false;
a.forEach(function(e){
b.forEach(function(k){
if(k.conversationWith==e.conversationWith)
{
bool=true;
k.message.push(e.message)
}
})
if(bool==false)
{
var obj={};
obj.conversationWith=e.conversationWith;
obj.message=[];
obj.message.push(e.message);
b.push(obj);
}
bool=false;
})
console.log(b)
Upvotes: 2
Reputation: 196
Array reduce approach
The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
use Array.prototype.reduce
to return an array of new objects created from source array with Array.prototype.some
and Array.prototype.filter
and Array.prototype.map
and Array.prototype.join
to fitler and join the messages together
https://repl.it/repls/TealVitalDatum
const LEROY = 'Leeroy Jenkins';
const convoWithMessage = [{
_id: '5d3eed4b8558ab0fc513a3b4',
subject: 'Subject',
message: 'Message',
newMessage: true,
from: '5d3b0585181c521610a15241',
fromName: 'John Doe',
to: '5d3b0749c9b633171fa62a48',
toName: 'Leeroy Jenkins',
conversationWith: 'John Doe'
}, {
_id: '5d3eed4b8558ab0fc513a3b4',
subject: 'Subject',
message: 'Message',
newMessage: true,
from: '5d3b0585181c521610a15241',
fromName: 'John Doe',
to: '5d3b0749c9b633171fa62a48',
toName: 'Leeroy Jenkins',
conversationWith: 'John Doe'
}].reduce((acc, item, index, array) => {
const isLeroy = d => d.toName === LEROY;
const leroys = acc.filter(isLeroy);
if (item.toName === LEROY) {
if (array.some(isLeroy)) {
const message = [item.message, ...leroys.map(i => i.message)];
return [...leroys.filter(isLeroy), { name: LEROY, message }]
}
}
return [...acc, { toName: item.toName, message: item.message }];
}, [])
console.log(convoWithMessage)
Upvotes: 0