Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Javascript convert comma seperated string of words to key value objects in array

I need to convert a list of email to a key value object within an array. I start with "email@mail.com,mike@example.com,john@gmail.com" and I want to end up with

[
   {"to": "email@mail.com"},
   {"to": "mike@example.com"},
   {"to": "john@gmail.com"}
]

Here's what I've tried

var req = {
  query: {
    personalize:
      "email@mail.com,mike@example.com,john@gmail.com"
  }
};
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@mail.com"
"1": "mike@example.com"
"2": "john@gmail.com"

After this I tried this one

var req = {
  query: {
    personalize:
      "email@mail.com,mike@example.com,john@gmail.com"
  }
};
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@mail.com": "to"
...
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

function mockTest() {
var req = {
  query: {
    personalize:
 "email@mail.com,john@gmail.com,me@example.com"
  }
};
var emailList = req.query.personalize;
console.log(createToList(emailList))
}

mockTest();

function createToList(emailString) {
  return emailString.split(",").map(to => ({to}));
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!