收藏
回答

小程序开发 list数组不会获取值?

  onLoad:function(options){
    let num = this.data.num;
    wx.getStorage({
      key'orderList',
      successfunction(res){
        // success
        console.log('已选美食',res.data)
      },
      failfunction() {
        // fail
      },
      completefunction() {
        // complete
      }
    })
   var  orderList = wx.getStorageSync('orderList');
   var  list=orderList.list;
   //console.log(list)
   //object 转 array
    var order = JSON.parse(options.order);
    var t_order = [];
    var t_total = 0;
    for(var k in order){
      if(order[k].num > 0){
          t_order.push(order[k]);
          t_total = t_total + order[k].cost*order[k].num; //计算总价格
      }
    }
    t_total=order.total;
    this.setData({
      total: t_total,
      orderList:list
    });
    console.log('数量list',list)
    num = list.length;
    console.log('数量num',num)
  },
打印出结果是下图

1、如何得到list的个数 num = list.length;这个得到结果是undefined
2、如何得到list的id,就是这个123  这个值是上一个页面传过来的选择值,是选择后的值,不是固定123list也可能{3251132}

想要得到list数组中的个数,和id

num = 3

listid1 = 1 ,listid2 = 2,listid3 = 3,
如果list是{3251132};listid1 = 3 ,listid2 = 4,listid3 = 13

大神们帮忙看下

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

3 个回答

  • 武曲心
    武曲心
    2022-05-12

    你打印的list是对象不是数组,自然没有list.length,t_total是什么意思?循环里面做了计算,反手就是被order.total赋值,num可以用Object.keys(list).length

    2022-05-12
    有用 1
    回复 1
    • 轻风
      轻风
      2022-05-12
      num 的问题解决了,谢谢
      2022-05-12
      回复
  • Practice in daily life
    Practice in daily life
    2022-05-12

    这个是个对象啊,对象是没有length属性的

    2022-05-12
    有用 1
    回复
  • 轻风
    轻风
    2022-05-12

    num 的问题解决了,

    listid1 = 1 ,listid2 = 2,listid3 = 3,
    如果list是{3251132};listid1 = 3 ,listid2 = 4,listid3 = 13
    

    这个,还是不会,获取。帮忙看下

    2022-05-12
    有用
    回复 1
    • 武曲心
      武曲心
      2022-05-12
      完全看不懂你这是啥规律
      2022-05-12
      回复
登录 后发表内容