向条件数组中的每个对象添加新值

时间:2020-11-01 04:04:02

标签: android react-native

我有一个称为“交易”的状态数组,其中包含类似于下面的数据;我想向其他API发出请求,并仅在满足特定条件时才从响应中添加一个值到交易状态数组。

例如;

仅将我从响应中获得的header_image值添加到deals状态数组中的对象,但前提是deals状态数组的值为isSteam=true

当前

[
 {
      "added":1567049314,
      "drm":[
         "steam"
      ],
      "expiry":1567443600,
      "game_code":"224280",
      "isSteam":true,
      "plain":"rpgmakervxacelite",
      "price_cut":80,
      "price_new":13.99,
      "price_old":69.99,
      "shop":{
         "id":"steam",
         "name":"Steam"
      },
      "title":"RPG Maker VX Ace Lite",
      "urls":{
         "buy":"https://store.steampowered.com/app/224280/",
         "game":"https://isthereanydeal.com/game/rpgmakervxacelite/info/"
      }
   }
 ---More Data---
 }
]

期望

[
     {
          "added":1567049314,
          "drm":[
             "steam"
          ],
          "expiry":1567443600,
          "game_code":"224280",
          "isSteam":true,
          "game_image": "https://steamcdn-a.akamaihd.net/steam/apps/224280/header.jpg?t=1540461421",
          "plain":"rpgmakervxacelite",
          "price_cut":80,
          "price_new":13.99,
          "price_old":69.99,
          "shop":{
             "id":"steam",
             "name":"Steam"
          },
          "title":"RPG Maker VX Ace Lite",
          "urls":{
             "buy":"https://store.steampowered.com/app/224280/",
             "game":"https://isthereanydeal.com/game/rpgmakervxacelite/info/"
          }
       },
      ---More Data---
     }
    ]

我有以下函数,我可以在其中发出请求,并希望在例如以下条件下将结果添加到状态数组中的每个对象中其中“ IsSteam = true”

getGameImage = () => {
      for (var key in this.state.deals) {
        if (this.state.deals[key]['isSteam']) {
          // Get Images
          axios.get('https://store.steampowered.com/api/appdetails?appids=' + this.state.deals[key]['game_code'])
          .then((response) => {
            var parsedData = JSON.parse(response['request']['_response'])
            for(var key in parsedData) {
              var result = this.state.deals.map(function(el) {
                var o = Object.assign({}, el);
                if(o['isSteam']) {
                  o.game_image = parsedData[key]['data']['header_image'];
                }
                return o;
                })
                this.setState({deals: result});
                console.log(result)
             }
            }).catch((err) => {
              console.log(err)
          })
          }        
       }
    }

问题是;它确实将图像的URL仅添加到IsSteam=true处的对象,但将相同图像URL(可能是它获得的第一个)添加到所有isSteam=true处的对象。

我希望为每个游戏显示正确的图像URL,并且当前交易数组和API响应之间的链接是游戏代码。

1 个答案:

答案 0 :(得分:0)

解决方案

getGameImage = async () => {
      for(var key in this.state.deals) {
        let duplicateArray = [...this.state.deals]
        if(this.state.deals[key]['isSteam']) {     
          await this.setGameImage(this.state.deals[key]['game_code'])
          duplicateArray[key].game_image = this.state.imageUrl;
        }
        else {
          duplicateArray[key].game_image = 'https://i.ytimg.com/vi/mYrKAMLbjYA/maxresdefault.jpg';
        }
        this.setState({deals: duplicateArray});
      }
      }