Javascript obj assignment/mutation in for-loops I have a working function achieving what I want to do, but, I feel that there is a better, concise, and appropriate way to achieve this but I can't quite put my finger on how.

Given an object with (n) recipients, duplicate the message so that there is only ever one recipient and every recipient receives a unique message-id.

here is how I achieved it:

const uuidGenerator = () =&gt; Math.random();

const message = {
  message: 'hello',
  recipients: ['John', 'Susie', 'Christian'],
}

function split(obj) {
  const messages = [];
  const recipients = obj.recipients;
  let copy = obj;

  for (let i = 0; i &lt; recipients.length; i++) {
    copy.id = uuidGenerator(),
    copy.recipient = recipients[i];
    copy.message = obj.message
    messages.push(copy);
    copy = {}
  }
  return messages;
}

const arrOfMessages = split(message)
console.log(arrOfMessages)


precisely I am not a fan of how at the end of each loop I am reassigning an obj. I feel... dirty, and it feels like the bigger an object could be the more expensive it could be... what if the message had Base64 video file within it? copying and reassigning feels like this could become more expensive (n2) in big O notation?

any help and clarification would be well appreciated!
I think this is what you're looking for

const uuidGenerator = () =&gt; Math.random();

const message = {
  message: 'hello',
  recipients: ['John', 'Susie', 'Christian'],
};

function split(obj) {
  const { message, recipients, ...remaining } = obj;
  return recipients.map((recipient) =&gt; {
    return { id: uuidGenerator(), message, ...remaining, recipient: recipient };
  });
}

const arrOfMessages = split(message);
console.log(arrOfMessages);


if there are too many values that you want to copy from the actual obj, then you can use the spread operator i.e., remaining in this case. 

However, I am not sure about how efficient this is. These are possible approaches: 
- Object.assign
- Spread Operator
- manually adding keys to base Object
Because, I have seen instances where each of these perform differently based on the sample size.

I remember that last approach is faster if your sample size is huge.