forEach遍历出来的数据 使用的时候为啥只显示最后一项呢?
发布于 6 年前 作者 hshen 13803 次浏览 来自 问答

forEach遍历出来的数据  使用的时候为啥只显示最后一项呢?

3 回复
 onClick3(e) {
    let that = this
    let json = that.data.CompanyMsg
    console.log(json);
    var datas = [];
    json.forEach(function (item, index{
      console.log(item);
      console.log(index);
      var data = {}; //每次循环的时候,新建对象
      data.msg = item;
      datas.push(data);
    })
    console.log(e);
    let info = JSON.stringify(datas)
    console.log(info);
    let jsons = JSON.parse(info);
    console.log(jsons);
    let copy = [];
    //遍历当前datas数组
    datas.forEach(function (item, index{
      console.log('公司名称:' + item.msg.CompanyName);
      console.log('下标' + index)
      copy.push(item)
      that.setData({
        gsName: item.msg.CompanyName
      })
      console.log('gsName:' + that.data.gsName)
      $wuxSelect('#wux-select3').open({
        value: that.data.value3,
        multipletrue//是否可多选
        // toolbar: {
        //   title: '请选择',
        //   confirmText: '确定',
        // },
        options: [{
            title: that.data.gsName,
            value: item.msg.ID,
          },
          // {
          //   title: json[1].CompanyName,
          //   value: json[1].ID,
          // },
          // {
          //   title: json[2].CompanyName,
          //   value: json[2].ID,
          // },
          // {
          //   title: json[3].CompanyName,
          //   value: json[3].ID,
          // }
        ],
        onChange(value, index, options) => {
          console.log('onChange', value, index, options)
          that.setData({
            value3: value,
            title3: index.map((n) => options[n].title),
          })
        },
        onConfirm(value, index, options) => {
          console.log('onConfirm', value, index, options)
          that.setData({
            value3: value,
            title3: index.map((n) => options[n].title),
          })
        },
      })
    })

  },

贴一下代码吧

回到顶部