收藏
回答

怎么将一个数组的某几项赋给另一个数组中的几项?

const db = wx.cloud.database({});
const cont = db.collection('question_data');
 
Page({
  data: {
    ne_1: [],
    ne_2: [],
    ne_3: [],
    ne_4: [],
    ne_5: [],
    ne_final: [],
    res: null,
    sum0: 0,
    id: 0
  },
 
 
  onLoad: function (options) {
    var _this = this;

    const db = wx.cloud.database();//下面将5个数据库的记录(均小于20条)赋给5个数组

    db.collection('question_group1').get({
      success: res => {
        console.log(res.data)
        this.setData({
          ne_1: res.data
        })
      }
    })
       
    db.collection('question_group2').get({
      success: res => {
        console.log(res.data)
        this.setData({
          ne_2: res.data
        })
      }
    })
 
    db.collection('question_group3').get({
      success: res => {
        console.log(res.data)
        this.setData({
          ne_3: res.data
        })
      }
    })
 
    db.collection('question_group4').get({
      success: res => {
        console.log(res.data)
        this.setData({
          ne_4: res.data
        })
      }
    })
 
    db.collection('question_group5').get({
      success: res => {
        console.log(res.data)
        this.setData({
          ne_5: res.data
        })
      }

    })

//每个数组取前i项赋给数组ne_final,最后的数组里需要20条记录 //下面是我写的错误的代码,怎么改都报错,不知道应该办

    for (let i = 0; i < 6; i++) {
      this.setData({
        'ne_final[sum0]': this.data.ne_1[i],
        sum0: this.data.sum0 + 1
      })
    }
    for (let i = 0; i < 4; i++) {
      this.setData({
        'ne_final[sum0]': this.data.ne_2[i],
        sum0: this.data.sum0 + 1
      })
    }
    for (let i = 0; i < 3; i++) {
      this.setData({
        'ne_final[sum0]': this.data.ne_3[i],
        sum0: this.data.sum0 + 1
      })
    }
    for (let i = 0; i < 4; i++) {
      this.setData({
        'ne_final[sum0]': this.data.ne_4[i],
        sum0: this.data.sum0 + 1
      })
    }
    for (let i = 0; i < 3; i++) {
      this.setData({
        'ne_final[sum0]': this.data.ne_5[i],
        sum0: this.data.sum0 + 1
      })
    }
  },

小程序新手,一直搞不懂数组,感觉和c语言的数组差别好大,求解答,谢谢!

回答关注问题邀请回答
收藏

1 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2019-11-17

    let ret = {

        sum0: this.data.ne_1.slice(0,5),

        sum1: this.data.ne_2.slice(0,3),

        sum2: this.data.ne_3.slice(0,2),

        sum3: this.data.ne_4.slice(0,3),

        sum4: this.data.ne_5.slice(0,2),

    }

      this.setData({

        ne_final:ret,

        sum0: ret.sum0.length+ret.sum1.length+ret.sum2.length+ret.sum3.length+ret.sum4.length

      })




    2019-11-17
    有用
    回复 2
    • 2019-11-17
      可是最后我取特殊值ne_final[1],发现ne_final里没有数据,是不是我还应该要加点啥?
      2019-11-17
      回复
    • o0o有脾气的酸奶
      o0o有脾气的酸奶
      2019-11-18回复
      你是要数组的话
      2019-11-18
      回复
登录 后发表内容
问题标签