I have a string like this:
`Name: XYZ
Age: 28
Email: [email protected]
Department: Test`
How can I convert this to JavaScript object like:
{Name: 'XYZ', Age: '28', Email: '[email protected]', Department: 'Test'}
I have a string like this:
`Name: XYZ
Age: 28
Email: [email protected]
Department: Test`
How can I convert this to JavaScript object like:
{Name: 'XYZ', Age: '28', Email: '[email protected]', Department: 'Test'}
2
Answers
The following Code should work fine for you
Use split to get an array of lines, then filter out empty lines. Finally, use split on each line to get the key and value, and use Object.fromEntries to turn key-value pairs into an object.