0

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
]
4
  • Edit your question with an example (use tool [<>]), we need: Your code, data example Commented Nov 11, 2021 at 14:19
  • I will edit so the final code I want will be clear Commented Nov 11, 2021 at 14:23
  • So can we assume both array lengths are identical.? Commented Nov 11, 2021 at 14:24
  • participantArray and messageArray are the same length Commented Nov 11, 2021 at 14:29

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.