官方文檔是這樣的:
changeItemInArray: function() { // you can use this way to modify a danamic data path this.setData({ 'array[0].text':'changed data' }) }, changeItemInObject: function(){ this.setData({ 'object.text': 'changed data' }); },
無奈自己寫的時候還是會遇到不能解決的問題,比如
this.setData({ 'array[0].text':'changed data' })
這里面的arry[o]是動態的,'array[index].text',像這樣顯然是不行的,在網上搜了搜,雖然不知其所以然,在這里記錄下來。
比如,要修改that.data.goods[0].price 的值,可以像這樣,
這里是一個動態的變量
var price = 'goods['+index+'].price' this.setData({ [price]:'changed data' })
例:
toggle: function (e) { // console.log(e) var index = e.target.id; let showConObj = 'catalog['+index+'].showCon'; console.log(showConObj); this.setData({ [showConObj]: !this.data.catalog[index].showCon }) }
