How to convert Array Alphabetical List with Headings
I have the below working code (from Stackoverflow), the issue is that the expected output should be
- <ul>
- <li>A</li>
- <li>Apple</li>
- <li>Apple2</li>
- </ul>
But it generates like below, Please do advise how to resolve this.
- <ul>
- <li>A</li>
- <li>Apple</li>
- <li>A</li>
- <li>Apple2</li>
- </ul>
var arr = ["Apple",
"Apple2", "Orange", "Pineapple"];
var $el = '<ul>'; $.each(arr, function(i, v) { $el += '<li id = "header">' + v.slice(0, 1) + '</li>'; $el += '<li id = "list" >' + v + '</li>'; }); $el += '<ul>'; $('body').append($el);