0

I need to convert a list of email to a key value object within an array. I start with "[email protected],[email protected],[email protected]" and I want to end up with

[
   {"to": "[email protected]"},
   {"to": "[email protected]"},
   {"to": "[email protected]"}
]

Here's what I've tried

var req = {
  query: {
    personalize:
      "[email protected],[email protected],[email protected]"
  }
};
var emailList = req.query.personalize;
var emailArr = emailList.split(",");
var emailObj = Object.assign({}, emailArr);
console.log(emailObj);

Here's what I ended up with

"0": "[email protected]"
"1": "[email protected]"
"2": "[email protected]"

After this I tried this one

var req = {
  query: {
    personalize:
      "[email protected],[email protected],[email protected]"
  }
};
var emailList = req.query.personalize;
var arr = emailList.split(",");
const res = arr.reduce((acc,curr)=> (acc[curr]='to',acc),{});
console.log(res)

This got me close, but backwards for what I wanted. Yielding a result like

"[email protected]": "to"
...
3
  • emailList.split(",").map(to => ({to})) Commented Jun 29, 2022 at 15:26
  • "[email protected],[email protected],[email protected]".split(',').map(to => ({to})) Commented Jun 29, 2022 at 15:26
  • Yes this is the solution I was missing! Thank you. Commented Jun 29, 2022 at 15:33

1 Answer 1

0

As Ivar and Amir pointed out in the comments, this solution works.

function mockTest() {
var req = {
  query: {
    personalize:
 "[email protected],[email protected],[email protected]"
  }
};
var emailList = req.query.personalize;
console.log(createToList(emailList))
}

mockTest();

function createToList(emailString) {
  return emailString.split(",").map(to => ({to}));
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.