0
\$\begingroup\$

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 = () => 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 < 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!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Review! Please edit your question so that the title describes the purpose of the code, rather than its mechanism. We really need to understand the motivational context to give good reviews. Thanks! \$\endgroup\$ Mar 11, 2020 at 9:55
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. Feel free to ask a new question with the improved code linking back to this one. \$\endgroup\$
    – Mast
    Mar 12, 2020 at 9:00
  • \$\begingroup\$ If you don't like the reassigning part, just declare it within the loop. That would become something like for(looping constructs) { const copy = obj; // ... everything else }. This way at the end of loop it gets auto destroyed \$\endgroup\$ Mar 13, 2020 at 13:16

1 Answer 1

2
\$\begingroup\$

I think this is what you're looking for

const uuidGenerator = () => Math.random();

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

function split(obj) {
  const { message, recipients, ...remaining } = obj;
  return recipients.map((recipient) => {
    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.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for the input, I was able to modify it a little differently, I am not sure how efficient it is either. but maybe you can weigh in on it? \$\endgroup\$
    – Ctfrancia
    Mar 12, 2020 at 8:38

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.