I've two comma separated strings, which i need to transform it to array of objects
{
"id": "1,2,3",
"name": "test 1, test 2, test 3"
}
How can I get the transformed output as object?
{
"selectedDataConcepts": {
"dataConcepts": [{
"id": "1",
"name": "test 1"
},
{
"id": "2",
"name": "test 2"
},
{
"id": "3",
"name": "test 3"
}
]
}
}
Tried multiple ways didn't work, would be glad if someone can provide solution
You can use String.prototype.split and get ids and names as array and then use it to get the desired result.
const data = { id: "1,2,3", name: "test1, test2, test3" };
const ids = data.id.split(',');
const names = data.name.replaceAll(' ', '').split(',')
const result = {
selectedDataConcepts: {
dataConcepts: ids.map((id, index) => ({id, name: names[index]}))
}
}
console.log(result);