-1

I'd like to convert this string:

"Allergies: adas \n other: asdasdfgfdfbfb \n hola:hola"

to an object like

{
"Allergies":"adas",
"other":"asdasdfgfdfbfb",
"hola":"hola"
}

How can I achieve this, including the removal of the whitespace?

2
  • 3
    Have you tried anything yourself? Commented Nov 4, 2022 at 21:59
  • Use split() multiple times. First to split it into lines at \n, then to split each line into key:value Commented Nov 4, 2022 at 22:02

1 Answer 1

0

I would do this by splitting into arrays:

const string = "Allergies: adas \n other: asdasdfgfdfbfb \n hola:hola";
console.log(Object.fromEntries(string.split('\n').map(inner => inner.split(':')).map(([s1, s2]) => [s1.trim(), s2.trim()])));

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.