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

151
Views
convert a sentence to an object with Key/Value Format js

I have a sentence like below:

mySentence = "12,alex \n" +"22,zac \n" +"41,sara \n" +"33,mike \n"

and want to convert into an object like below:

{
"12":"alex",
"22":"zac",
"41":"sara",
"33":"mike"
}

any solution would be my appreciated

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Try this

var mySentence = "12,alex \n" +"22,zac \n" +"41,sara \n" +"33,mike \n"
mySentence = Object.fromEntries(mySentence.trim().split(' \n').map(v => v.split(',')))
console.log(mySentence)

about 4 years ago · Juan Pablo Isaza Report

0

You can try this:

const mySentence = "12,alex \n" +"22,zac \n" +"41,sara \n" +"33,mike \n";
const object = {};
mySentence.split('\n').filter(e => e != '').forEach(e => {
  const str = e.trim().split(',')
  const key = str[0];
  const value = str[1];
  object[key] = value;
});
console.log(object)
about 4 years ago · Juan Pablo Isaza Report

0

You could use a package like QS or https://www.npmjs.com/package/url-search-params-polyfill to support any browsers like IE and Node.js as well.

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!