💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 小程序[云开发](https://so.csdn.net/so/search?q=%E4%BA%91%E5%BC%80%E5%8F%91&spm=1001.2101.3001.7020)云函数更新数组的某一项 #### 比如说在[数组](https://so.csdn.net/so/search?q=%E6%95%B0%E7%BB%84&spm=1001.2101.3001.7020)的第0项的某个元素进行更新 ~~~ db.collection('todos').doc('test').update({ data: { 'user.1.age': 30 }, }) ~~~ 但是1得用变量代替,如下: ``` var value = 'pinglun.1.reply' var num = 1; var value = 'pinglun.' + num + '.reply' db.collection('test').where({ _id:event._id, }).update({ data:{ //本来这里的 [value] 值应该是 'pinglun.1.reply', 但是这里的1(也就是pinglun这个数组的下标)是前端页面动态传来,而 'pinglun.1.reply' 这样的形式是无法使用变量的,所以我选择在前端页面就拼接成字符串,然后传一个变量到云函数,而使用变量的方法就是加一个中括号,[value] [value]:_.push({ openId:event.openId, nickName:event.nickName, avatarUrl:event.avatarUrl }) } }) ``` ### 其他案例: ``` set_db_send_words_text(words_index0,words_index1,words_text) {    var that = this;     //获取哪个用户发的     var user = this.data.userInfo;     var user_list = this.data.info.user_list;     var user_index = 0;     for(var i in user_list){         if (user_list[i].openid == user._openid) {             user_index = user._openid;             break;         }     }      that.data.db.collection('dice_many').where({         _id:that.data.room_id,         user_list: that.data.db.command.elemMatch({             openid:that.data.userInfo._openid,         })     }).update({        // data 字段表示需新增的 JSON 数据        data: {             "user_list.$.words_index0":words_index0,             "user_list.$.words_index1":words_index1,             "user_list.$.words_text":words_text,             "user_list.$.words_time":new Date().toLocaleDateString() + ' '+ new Date().toLocaleTimeString(),             play_words_user_index:user_index,             play_words_time:new Date().toLocaleDateString() + ' '+ new Date().toLocaleTimeString()//播放声音用的          },        success: function (res) { }      }) }, ```