对象数组的值修改问题?

小程序 文章 2020-08-11 11:41 650 0 全屏看文

AI助手支持GPT4.0

对象数组的值修改问题?Is the value of the object array modified?

对象数组的值修改问题?

本人在JS data中定义了一个对象数组:

outlist:[

      {

        id:1,

        title:'out外形',

        out:true

      },

      {

        id:2,

        title:'尺寸',

        out:true

      },

      {

        id:3,

        title:'重量',

        out:true

      },

]

随便通过一个按钮点击修改数组里的某个值,如下,console可以正常打印,为何在赋值outlist[0.out时编译器竟然报错?貌似很简单,自己没找出为何错了?

click(){

    console.log("outlist",this.data.outlist[0].out)

    this.setData({

      outlist[0].out:false

    })

 

  },


I define an object array in JS data: outlist: [{ID: 1, Title: 'out shape', out:true }, {ID: 2, Title: 'size', out:true }, {ID: 3, Title: 'weight', out:true },] click a button to modify a value in the array. As follows, the console can print normally. Why does the compiler report an error when assigning outlist [0.out)? It seems very simple. I didn't find out why I was wrong? click(){ console.log ("outlist", this.data.outlist [0].out) this.setData ({ outlist[0]. out:false }) },

回答:

上上签:

这是我写的,是没有没问题的

Jason_亮 🇨🇳:看不懂
上上签:哈哈,你可以理解为字符串外边加了一个[]成数组了,实现局部刷新或者是局部改变数据
Jason_亮 🇨🇳:但代码看起来,好像不是我的问题的对应解决方案啊
上上签:你的 this.setData({
      outlist[0].out:false
    })中的outlist[0].out报错,写法上有问题,写成我俩这种应该就不会报错了
卢霄霄:他俩说的是一个意思,都是对的,请参看下面文档最底下的示例代码
https://developers.weixin.qq.com/miniprogram/deveference/api/Page.html#Page.prototype.setData(Object%20data,%20Function%20callback)
Jason_亮 🇨🇳:谢谢了
xplee:

这样写试试

const index = 0let key = 'outlist[' + index + '].out'this.setData({  [key]: false})


Jason_亮 🇨🇳:谢谢了
木有鱼丸:

你写错了 我的哥

-EOF-

AI助手支持GPT4.0