【问题标题】:React multidimensonal array filter/find反应多维数组过滤器/查找
【发布时间】:2018-05-29 16:39:18
【问题描述】:

我有一个多维数组(与玩家匹配的列表),我需要过滤这个数组以仅获取唯一的玩家对象。

以下代码有效,但我预期的结果不同。

Const aPlayer=matchs
  .filter(match => match.players.find(player => player.id     
 ===id))

变量 aPlayer 包含该玩家的所有匹配项。

但我只需要玩家对象数据。

【问题讨论】:

    标签: reactjs react-native multidimensional-array filter


    【解决方案1】:

    我不确定您的数据结构是否如下:

    Matches: List<Match>
    Match:   {
        ...,
        players: List<Player>
    }
    Player: {
       id: Index,
       ...
    }
    

    如果是这样,你可以这样做:

    // still contains duplicates, but that's not the issue here
    const allPlayers = matchs
        .map(x => x.players)
        .reduce(x => (dest, val) => dest.concat(val), []);
    
    const player = allPlayers.find(x => x.id === id);
    

    【讨论】:

    • 不行,console.log(allPLayers)= "function(dest,val) { return dest.concat(val);}"
    • 究竟什么不起作用,错误/结果是什么?
    • Const allPlayers=matchs .filter(match => match.players.find(player => player.id ===id)) 跟着 const player = allPlayers.find(x => x. id === id);做好工作,谢谢
    猜你喜欢
    • 2018-07-07
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多