首页 > 解决方案 > Uncaught (in promise) TypeError: filter is not a function

问题描述

我正在尝试从 API 获取数据。我将数据过滤到每个用户名中,因此我可以从每场比赛中获得杀戮、死亡和爆头。我希望输出是用户名,然后所有的杀戮,死亡,爆头相乘,但是在我可以乘以数据之前,我得到了这个错误

playerstats.js:45 Uncaught (in promise) TypeError: matchlistResponsestats.filter is not a function
    at getmatchdata (playerstats.js:45)

而且我不知道出了什么问题

   async function getplayerdata1() {
    // Get current room
    const response = await fetch(url);
    const { players } = await response.json();
    return Promise.all(players.map(async (player, playerIndex) => {
      const { username, id } = player;
      const playerid = player.id
      console.log(playerid + username)
      // read user matches
      const response = await fetch(`https://api.site.com/user_profile/get_latest_matches?_=&id=${id}&page=1`);
      const matchlist = await response.json();
      return Promise.all(matchlist.map(async (match, matchIndex, array ) => {
        const { id } = match;
        // Read match stats
        const matchlistResponse = await fetch(`https://api.site.com/match/get?_=&id=${id}`);
        const matchlistResponsestats = await matchlistResponse.json();
        let matchArray = matchlistResponsestats.players.length
        async function getmatchdata() {

        for (var i = 0; i < matchArray; i++) {
          if(matchlistResponsestats.players[i].username === username) {
            matchlistResponsestats.filter((item) => item.username === username).map((item) => {
               return {
                 item: [{id: item.id, kills: item.kills, deaths: item.deaths, headshots: item.headshots}]
               }
             })
           }
        }
      }
      let playerstats = await getmatchdata()
      console.log(playerstats)

      }));
    }));
  }
  getplayerdata1()
  }

更新 console.log( MathclistResponsestats )

{id: 1862369, region_id: 0, country_id: 210, map_id: 3, flags: 0, …}
{id: 1855913, region_id: 0, country_id: 57, map_id: 5, flags: 0, …}
{id: 1856079, region_id: 0, country_id: 210, map_id: 4, flags: 0, …}

等等

标签: javascriptarrays

解决方案


推荐阅读