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

121
Views
split the string and convert to array of objects javascript

I have a string data, I need to convert to array of objects in javascript

Below the code tried

var splitarr = sample.split('|');
var result = splitarr.map(e=>{
  details: e,
  values: splitarr[e]
})


var sample =  "finance=60|IT=88|sales=50"


Expected Output

[
  {"details": "finance","tag":60},
  {"details": "IT","tag":88},
  {"details": "sales","tag":50}
]

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

0

You need another split by =, try:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result = splitarr.map(e=>{
  let keyVal = e.split('=');
  return { details: keyVal[0], tag:keyVal[1] }
})

console.log(result);

Also when you return object from arrow function you neeed to use brackets (), otherwise it will be considered as block of code, try:

var sample =  "finance=60|IT=88|sales=50";
var splitarr = sample.split('|');

var result1 = splitarr.map(e=>{
  details: e
})

console.log(result1);

var result2 = splitarr.map(e=>({
  details: e
}))

console.log(result2);

about 4 years ago · Juan Pablo Isaza Report

0

Another way of transforming string to object.

const sample =  "finance=60|IT=88|sales=50";

const result = sample
  .split('|')
  .map((e) => e.split('='))
  .map(([details, tag]) => ({ details, tag }));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}

about 4 years ago · Juan Pablo Isaza Report

0

Observations :

  • Instead of values property it should be tag.
  • As splitter will be a simple array of elements. This splitarr[e] will not work or not exist.

You can use String.split() method to get the details and tag value.

Working Demo :

const sample =  "finance=60|IT=88|sales=50";

const splitarr = sample.split('|');

const result = splitarr.map((e) => {
    return {
    details: e.split('=')[0],
    tag: e.split('=')[1]
  }
});

console.log(result);

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!