首页 > 解决方案 > for循环和等待关键字

问题描述

我有一个使用 await 关键字的 for 循环,它用于检索应用程序以填充 2D 数组:

 // initial a 2D array
  const filteredGameApps: InstalledApp[][] = []; 

  for (let i = 0; i < length; i++) {
    // get apps by Id
    const apps = await getSuggestedGameAppsById(suggestedGameAppIds[i]);

    let filteredApps = apps.filter(
      app =>
        highlyRecommendations.some(highlyRecommendation=> highlyRecommendation.id === app.id)
    );

    filteredApps = filteredApps.slice(0,6);

    filteredGameApps[i] = filteredApps;
  }

它不起作用,它仅在删除 for 循环时才起作用,例如:

    const filteredGameApps: InstalledApp[][] = []; 
    const i = 0;

    // get apps by Id
    const apps = await getSuggestedGameAppsById(suggestedGameAppIds[i]);

    let filteredApps = apps.filter(
      app =>
        highlyRecommendations.some(highlyRecommendation=> highlyRecommendation.id === app.id)
    );

    filteredApps = filteredApps.slice(0,6);

    filteredGameApps[i] = filteredApps;
  

我在这里错过了什么吗?

标签: reactjstypescript

解决方案


推荐阅读