微信小程序向原數組添加數組

concat():向原數組後面添加數組

數組newarray:

var newarray = [{ value: '02', code: '港澳臺證' }]

原數組cardTypeDict ,在數組後面添加newarray數組:

    this.setData({
      cardTypeDict: this.data.cardTypeDict.concat(newarray),
    })

本來想添加多次,如下操作:

   this.setData({
      cardTypeDict: this.data.cardTypeDict.concat(newarray),
      cardTypeDict: this.data.cardTypeDict.concat(newarray)
    })

發現以上的操作,最終的數組還是隻有這樣的值,並不會出現2條。

[{ value: '02', code: '港澳臺證' }]

但是分開進行2次this.setData操作則可以。

所以建議是先用將值都加在一個數組裏,然後在this.setData方法中進行concat方法賦值。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章