How to add to an array in javascript?

This question already has an answer here:

  • How to append something to an array? 25 answers

  • First, the method to add element to array is called push , not add

    Second, when you do join on array, you get string, which doesn't have methods like push or add

    If you really want to do this, you need this code:

    var lootArray = []
    
    lootArray.push("ramdom1");
    
    lootArray = lootArray.join("<br>");
    
    document.getElementById("text_loot").innerHTML = lootArray;
    

    Replace

    lootArray.add("ramdom1");
    

    with

    lootArray.push("ramdom1");
    

    use push method like this. hope it will works

    lootArray.push("ramdom1"); 
    
    链接地址: http://www.djcxy.com/p/17948.html

    上一篇: 将元素添加到数组

    下一篇: 如何添加到JavaScript中的数组?