小程序 多层次对象数组的赋值、动态赋值

1、给对象赋值


  data: {
    form: {
      Name: '',
      IDCard: '',
      Sex: '',
    }
  },

对单个属性赋值 

this.setData({
      'form.Name':'章三',
      ['form.Sex']:'女',
    })

动态赋值

onChangeDate(e) {
    let field = e.currentTarget.dataset.field;
    this.setData({
      [`form.${field}`]: e.detail.data
    })
  },

field 是wxml上通过data-field传过来的

2、给对象数组赋值

Questions: [
      {
        name: 'HighStrung',
        question: '今天你的心情好吗?',
        score: null,
        options: [
          {
            ans: '没有',
            val: 0,
            scoreOpt: null,
          },
          {
            ans: '有几天',
            val: 1,
            scoreOpt: null,
          }
        ]
      }
    ]

 对单个属性赋值:

this.setData({
   [`Questions[${index}].score`]: val
})
this.setData({
   [`Questions[${index}].options[${idx}].scoreOpt`]: val,
})

你可能感兴趣的:(#,小程序,前端,小程序)