Home > OS >  Javascript convert array value string to array
Javascript convert array value string to array

Time:07-25

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

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

CodePudding user response:

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)

CodePudding user response:

Ok, I'll give you my best idea:

let arr = [];
data.forEach(ele => arr.push(ele.split(":")));

that will return [ [ 'genStatus', ' INPROGRESS,DONE' ], [ 'status', ' ACTIVE,DEACTIVATE' ] ]

from here you should be able to figure out the rest. You will have to create another empty array and push all the arr[i][0] values as an empty array. Then iterate through the rest and push it inside those empty arrays.

CodePudding user response:

I shared my approach here. Hope you will understand it.

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

// Approach 1
const obj = data.reduce((acc, curr) => {
  const [key, data] = curr.split(":")
  acc[key] = data.trim().split(",")
  return acc;
}, {})

console.log(obj)

// Approach 2
const obj2 = data.map(el => {
  const [key, data] = el.split(":")
  const obj = {};
  const dataArray = data.trim().split(",").map(d => ({
    name: d
  }))
  obj[key] = dataArray
  return obj;
})

console.log(obj2)

CodePudding user response:

Here's what I came up with:

function parse_data(data) {
    var result = {}
    data.forEach(e => {
       e = e.split(":");
       var key = e[0];
       var value = [];
       e[1].split(",").forEach(x => {value.push(x.trim())});
       result[key] = value;
    });

    return result;
 }

Output:

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

Another method:

function another_parse_data(data) {
    var result = []
    data.forEach(e => {
        e = e.split(":");
        var key = e[0];
        var value = [];

        e[1].split(",").forEach(x => {value.push({"name": x.trim()})});

        var presult = {}
        presult["name"] = key
        presult["data"] = value

        result.push(presult)
      });

    return result;
 }

Output:

[{"name":"genStatus","data":[{"name":"INPROGRESS"},{"name":"DONE"}]},{"name":"status","data":[{"name":"ACTIVE"},{"name":"DEACTIVATE"}]}]
  • Related