微信小程序中使用new Map()会报错,原因是基础库版本的问题,能解决一下吗?
发布于 6 年前 作者 wuqiang 397 次浏览 来自 问答
function addZero(val) {
  if (val < 10) {
    return '0' + val;
  } else {
    return val;
  }
}
function test() {
  const a =  [{amount: 1,
    createTime: 1590394393000,
    flowNo: "2020052500000001",
    state: "SUCCESS"},
    {amount: 1,
      createTime: 1590994395000,
      flowNo: "200052500000001",
      state: "SUCCESS"}];
  const monthObj = new Map();
  a.forEach(item => {
    const date = new Date(item.createTime);
    const year = date.getFullYear();
    const month = addZero(date.getMonth() + 1);
    const day = addZero(date.getDate());
    const hour = addZero(date.getHours());
    const minute = addZero(date.getMinutes());
    item.yearmonth = year + '年' + month + '月';
    if (!monthObj.has(item.yearmonth)) {
      monthObj.set(item.yearmonth, []);
    }
    item.day = month + '月' + day + '日' + ' ' + hour + ':' + minute;
    console.log(monthObj.get(item.yearmonth));
    monthObj.get(item.yearmonth).push(item);
  });
  console.log(monthObj, 23);
}
test();
 

以上是代码示例,在调试基础库为2.0.8版本可以运行,在2.0.9及以上版本会报错

在2.0.9版本报错如下:

在2.2.4版本报错如下:

在2.11.0版本下报错如下:

希望帮忙解决一下

1 回复

在2.11.0下试过了,并没有出错,应该不是Map本身的问题。猜测你是不是把test()搞成无限递归调用了

回到顶部