Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript convert array value string to array

How to convert this array string value to an array.

 data = ["genStatus: INPROGRESS,DONE", 
    "status: ACTIVE,DEACTIVATE"]

the expected output should be like this:

data = {
    genStatus: ["INPROGRESS","DONE"],
    status: ["ACTIVE", "DEACTIVATE"]
}

or should be like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = [{ name: 'genStatus', data: [{name: 'INPROGRESS'}, {name: 'DONE'}]},
{ name: 'status', data: [{name: 'ACTIVE'}, {name: 'DEACTIVATE'}]}]

>Solution :

const data = ["genStatus: INPROGRESS,DONE", "status: ACTIVE,DEACTIVATE"]

const transformed = data.reduce((object, item) => {
  // get key and value
  const [key, value] = item.split(' ')
  // assign key and split value
  object[key] = value.split(',')
  return object
}, {})

console.log(transformed)
const data = ["genStatus: INPROGRESS,DONE", "status: ACTIVE,DEACTIVATE"]

const transformed = data.map(item => {
  const [key, value] = item.split(' ')
  return {
    name: key,
    data: value.split(',').map(value => ({ name: value }))
  }
})

console.log(transformed)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading