Himas Rafeek
Posted on: 1 year ago

Javascript array To Html List Function

This snippet converts the elements of an array into “ tags and appends them to the list of the given ID.


                                          const arrayToHtmlList = (arr, listID) =>
  (el => (
    (el = document.querySelector('#' + listID)),
    (el.innerHTML += arr.map(item => `<li>${item}</li>`).join(''))
  ))();
  
arrayToHtmlList(['item 1', 'item 2'], 'myListID');