How convert items li into json? (JS)

How convert items li:

<ul id='list'>
    <li>city1</li>
    <li>city2</li>
    <li>city3</li>
    <li>city4</li>
</ul>

‘["city1","city2","city3","city4"]’

>Solution :

const list = document.getElementById("list"),
      cities = Array.from(list.children).map(each => each.innerHTML),
      citiesJSON = JSON.stringify(cities)

console.log(citiesJSON)
<ul id='list'>
  <li>city1</li>
  <li>city2</li>
  <li>city3</li>
  <li>city4</li>
</ul>

Leave a Reply