How to convert elements of an array into <li> tags?

const arrayToHtmlList = (array, listClass) =>
  (el => (
    (el = document.querySelector('.' + listClass)),
    (el.innerHTML += array.map(item => `<li>${item}</li>`).join(''))
  ))()
  
arrayToHtmlList(['hello', 'world', '42'], 'classOfTheList')
Copy

September 29th, 2019

# Append array elements to HTML list

Add to bookmarks

This snippet converts all elements of an array to a list of <li> tags and appends them to the list of the given class.

HomeBookmarks