I am trying to display a string containing a list of items in a specific format inside a div
tag using JavaScript. The string contains several items that I want to display as a bulleted list. Here’s the example string:
const items = "These are the items:n1. applen2. mango";
I want to format the string so that it appears as follows inside the div
tag:
These are the items:
1. apple
2. mango
I am using React and Tailwind CSS, and this question pertains to a React component.
3
Answers
const string = "These are the items:n1. applen2. mango"; console.log(string.split("n"));
You should parse your string in order to have a list.
A very simple parse would be :
items.split('/n')
and you would receive
["These are the items:", "1. apple", "2. mango"]
which is now easy to display
You can pass it as
innerText
: