A good JavaScript to add/remove items from/to array?

前端 未结 4 1887
名媛妹妹
名媛妹妹 2021-02-06 10:30

folks! Today I created this script that has the following functionality:

  • add new items to array
  • list all items from the array
  • remove an item from
4条回答
  •  我寻月下人不归
    2021-02-06 10:58

    Your problem isn't the arrays, your problem is this code:

    node.innerHTML += newFood;
    

    This code is very, very, very slow. It will traverse all exising DOM nodes, create strings from them, join those strings into one long string, append a new string, parse the result to a new tree of DOM nodes.

    I suggest to use a framework like jQuery which has methods to append HTML fragments to existing DOM nodes:

    var parent = $('#foods');
    ...
    for (var i = 0; i < foodList.length; i++) {
        parent.append( "
                    
                    0
                  
                       
                    
                   讨论(0)
                  
                                                      
                  
                  
提交回复
热议问题