0

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,
} 

1 Answer 1

2

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.