微信小程序中如何修改数组指定元素(或对象)

版权声明: https://blog.csdn.net/qq_37959401/article/details/80918172

1、更改数组中的值
可以改变数组中某一个特定下标的值


//数组
paraList:[{txt:'123',chose:false},{txt:'1234',chose:false}]
//细节
let choseChange = "paraList[" + index + "].chose"
_this.setData({
[choseChange]: true,
})
//paraList:[{txt:'123',chose:true},{txt:'1234',chose:false}]

2、更改对象中的值

//对象
userInfo: { 
sex: '',
name: '',
phone: '',
code: '',
sexTxt:'请选择你的性别',
nameTxt:'名字不能为空',
phoneTxt: '手机号不能为空',
codeTxt: '获取验证码',
codeErrTxt:'验证码不能为空'
},
//细节
let userSex = "userInfo.sex"
this.setData({
[userSex]: '1'
})
//更改后数据
userInfo: { 
sex: '1',
name: '',
phone: '',
code: '',
sexTxt:'请选择你的性别',
nameTxt:'名字不能为空',
phoneTxt: '手机号不能为空',
codeTxt: '获取验证码',
codeErrTxt:'验证码不能为空'
}

猜你喜欢

转载自blog.csdn.net/qq_37959401/article/details/80918172