I have two arrays which id like to merge into one array, however, the arrays have some complexities which I can't wrap my head around.... In short, I have an array of "last message" from a conversation, and I have an array of participants from that conversation.
The "messages" array looks like this
messageArray =
[
[
{ message } <=== convo 1
],
[
{ message } <=== convo 2
],
]
The "participant" data array looks like this
participantArray =
[
[
{participant},{participant} <=== participants in convo 1
],
[
{participant},{participant},{participant} <=== participants in convo 2
]
]
What i'd like to have is
mergedArray =
[
[ { message }, [ { participant }, { participant } ]] <=== conversation 1
],
[
[ { message }, [ { participant }, { participant } ]] <=== conversation 2
]
CodePudding user response:
Based on your description, you can map the first array, and use the index to get the associated items in the second array.
eg.
const messages = [
["Helo World, Messsage One"],
["Goodbye, fair well"],
];
const users = [
["Bob", "Jane", "Harry"],
["Mike", "John", "Amanda"]
];
const joined = messages.map((m, ix) => {
return [m[0], users[ix]];
});
console.log(joined);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>