如何把forEach循环改成for循环

如何把下面forEach循环改成for循环:

v.cityData.forEach(function(e) {
        let name = e.name.substr(0, 1);
        let len = e.cities.length;
        v.anchorMap[name] = y;
        y -= titleHeight + len * itemHeight;
      });
阅读 4.7k
3 个回答
for(let i = 0; i < v.cityData.length; i++) {
    let e = v.cityData[i]
    let name = e.name.substr(0, 1);
    let len = e.cities.length;
    v.anchorMap[name] = y;
    y -= titleHeight + len * itemHeight;
}

回答完这个问题之后有种难以言表的心情。。。

for (let i = 0, iLen = v.cityData.length; i < iLen; i++) {
        let e = v.cityData[i];
        let name = e.name.substr(0, 1);
        let len = e.cities.length;
        v.anchorMap[name] = y;
        y -= titleHeight + len * itemHeight;
}
for(let e of v.cityData) {
    let name = e.name.substr(0, 1);
    let len = e.cities.length;
    v.anchorMap[name] = y;
    y -= titleHeight + len * itemHeight;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题