zoukankan      html  css  js  c++  java
  • [转] 小程序修改数组中对象的某个值或者修改对象值

    小程序中获取当前data定义的值,用this.data.xxx

    setData的时候要修改的值是不需要加this.data.xxx的,直接xxx,

    一般直接修改data的值直接修改,修改数组中对象的值或者对象的属性值都要先转为字符串再加中括号,如果有变量可以用ES6的模版字符串反单引号或者字符串拼接一下。

    复制代码
    Page({
      data: {
        currentValue:"aa",
        todoLists:[
          {
            detail:"",
            date:"",
            location:"",
            priority:"",
            remark:"",
            dateStatus:false,
            locationStatus:false,
            dateRepeat:false,
            completeStatus: false,
            currentInput:'',
          },
          {
            detail: "",
            date: "",
            location: "",
            priority: "",
            remark: "",
            dateStatus: false,
            locationStatus: false,
            dateRepeat: false,
            completeStatus: false,
            currentInput:'',
          }
        ],
        aa:{
          a:1,
          b:2
        }
      },
      tickToComplete:function(e){
        //修改数组中对象的值
        let index = e.currentTarget.dataset.index;
        let completeStatus = `todoLists[${index}].completeStatus`;
        this.setData({
          [completeStatus]: !this.data.todoLists[index].completeStatus
        })
        //修改对象中的属性值
        this.setData({
          ['aa.a']: 3
        })
        console.log(this.data.aa.a); //3
        //修改普通data值
        this.setData({
         currentValue: "bbb"
        })
      }, 
    复制代码

  • 相关阅读:
    Dragon(preview)
    Release My New Single Album: Open The Door
    Writing REYES
    Expecting New Design: MiniMemMan
    never use virtual function for primitives!
    far.md
    ACL使用.md
    什么是技术,技术是什么
    Js客户端判断上传文件的类型
    color table
  • 原文地址:https://www.cnblogs.com/chris-oil/p/10111661.html
Copyright © 2011-2022 走看看