search

Home  >  Q&A  >  body text

javascript - js array accumulation problem


val is a parameter, and a value is passed every time you click, but the array does not accumulate. I don’t know why.

phpcn_u1582phpcn_u15822713 days ago933

reply all(2)I'll reply

  • typecho

    typecho2017-06-26 11:00:45

    Because the array you accumulate is new every time, arr is reassigned to ['s', 'd'] every time.

    reply
    0
  • 天蓬老师

    天蓬老师2017-06-26 11:00:45

    Just define var arr = [] outside the current function, similar to this:

    var arr = ['s', 'd']
    
    function addArr(val){
        arr.push(val)
    }

    reply
    0
  • Cancelreply