Reputation: 271
I want to convert string of arrays to an object.
input:
const test = [
' data1: 123',
' data2: 3524',
' data3: 5142',
' data4: 84521'
]
output:
{
data1: 123,
data2: 3524,
data3:5142,
data4: 84521,
}
Upvotes: 0
Views: 136
Reputation: 9300
Use Object.fromEntries()
and map the array to a fitting format. You can for instance do that with Array.prototype.split()
.
const test = [
'data1: 123',
'data2: 3524',
'data3: 5142',
'data4: 84521'
];
const result = Object.fromEntries(test.map((e) => e.split(": ")))
console.log(result);
Upvotes: 2