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,
}
Use Object.fromEntries()
and map the array to a fitting format. For instance with used 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);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments