Insert from list into array

前端 未结 2 477
天涯浪人
天涯浪人 2021-01-25 15:39

I wanted to know how I can return an array of names currently on the leaderboard.

This is the code I have:

相关标签:
2条回答
  • 2021-01-25 16:17

    You can fetch the li values into an array with this line of code:

    return Array.from(document.querySelectorAll('#top10>li'), li => li.textContent)
    

    If the intention is to limit that result array to maximumResults elements, then you can use slice:

    return Array.from(document.querySelectorAll('#top10>li'), li => li.textContent)
                .slice(0, maximumResults)
    
    0 讨论(0)
  • 2021-01-25 16:23

    This could be done with react very easy, but in this case all you need is some jquery!:

    var array = [1, 2, 4, 5, ,6 ,7 ,8] /// here you can update with own elements
    
    $(document).ready(function() {
        for (var i = 0; i < array.length; i++) {
             $("ol").append("<li>" + array[i] + "</li>")
             }
      })
    

    Demo here: http://jsfiddle.net/n5phf/405/

    0 讨论(0)
提交回复
热议问题