小程序怎么在setData方法中,给一个对象赋值一个数组指定下标的字段?
小程序怎么在setData方法中,给一个对象赋值一个数组指定下标的字段?How can a small program assign an object to an array with a specified subscript field in the setData method?
小程序怎么在setData方法中,给一个对象赋值一个数组指定下标的字段?小程序怎么在setData方法中,给一个对象赋值一个数组指定下标的字段?
How does the applet assign an array to an object and specify the subscript field in the SetData method?
回答:
let index=1this.setData({ ['a['+index+']']: 1})
全部读出来,改动,整体setdata
上码
Object.keys 可以获取数组下标
let a = ['apple', 'banner']Object.keys(a) // ['0', '1']
this.data.obj.key = arr[index]
this.setData({
obj: this.data.obj
})
你的问题有点读不懂,下面列举两种,x是对象变量名,y是x属性名,z是数组,index是z下标
1、this.setData({ 'x.y':z[index] })
2、let obj = x;x[index] = z[index];this.setData({ x:obj })
const index = 3;const updateKey = `arr[${index}]`;this.setData({ [updateKey]:"newValue",})
所以你不应该先贴你的代码?