Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中混合两个对象数组信息的算法? 问题_Javascript_Algorithm_Firebase - Fatal编程技术网

javascript中混合两个对象数组信息的算法? 问题

javascript中混合两个对象数组信息的算法? 问题,javascript,algorithm,firebase,Javascript,Algorithm,Firebase,我有这个数组 const cards = [ {id: "29210z-192011-222", power: 0.9}, // Card A {id: "39222x-232189-12a", power: 0.2} // Card B ... ] 我的firestore数据库如下所示: [{userId: "219038210890-234-22-a", power: 0.9}, {userId: &quo

我有这个数组

const cards = [
    {id: "29210z-192011-222", power: 0.9}, // Card A
    {id: "39222x-232189-12a", power: 0.2} // Card B
    ...
]
我的firestore数据库如下所示:

[{userId: "219038210890-234-22-a", power: 0.9}, {userId: "519aa821Z890-219-21-e", power: 0.2}]
卡片(集合)->cardId(文档)->用户ID(文档数据中卡片所有者的“外键”)

因此,我为阵列中的每张卡获取相应的所有者:

const documentRefs = cards.map((card) =>
    firestore.collection("cards").doc(card.id)
);

const rivalPlayers = db.collection("cards")
                  .getAll(...documentRefs)
                  .then((docs) => {
                      return docs.map(doc => {
                         console.log(doc.data); // { userId: "219038210890-234-22-a" }  
                         /*
                            TODO - Here I need to return an object containing the userId (from doc.data)
                            and the power of the card which referenced him
                         */
                       })
                   })
                   .catch(err => throw err);
正如您在代码中看到的,我需要一个数组“rivalPlayers”,如下所示:

[{userId: "219038210890-234-22-a", power: 0.9}, {userId: "519aa821Z890-219-21-e", power: 0.2}]
Pd:此数组不能包含当前用户,因此我对其进行筛选

问题: 有人知道在这种情况下如何混合阵列信息吗?我的意思是,在我从firebase映射文档的那一刻,我有userId(对应于cards数组中的一张卡)和carid(doc.id,因为我数据库中的每个文档id都是一个卡id),但我不知道如何以良好的性能获得每种电源

我希望这个解决方案是用现代javascript编写的。谢谢

我最好的方法
例如,试一试。

将所有功率值放在一个映射中,这样您只需在卡中迭代一次,而无需多次运行
过滤器()

const cards=[{id:“29210z-192011-222”,功率:0.9},{id:“39222x-232189-12a”,功率:0.2}];
constpowermap=newmap(cards.Map({id,power})=>[id,power]);
const wantedId=“39222x-232189-12a”

console.log('powerforid:',wantedId,'is:',powerMap.get(wantedId))
谢谢。这就是我要找的。
const cards = [
    {id: "29210z-192011-222", power: 0.9}, // Card A
    {id: "39222x-232189-12a", power: 0.2} // Card B
]

const usersIds = [
  {
    id: "39222x-232189-12a", // Firebase doc.id === card id
    data: {
      userId: "329u4932840" // Id of the owner of the card
    }
  },
  {
    id: "29210z-192011-222", // Firebase doc.id === card.id
    data: {
      userId: "8u4394343" // Id of the owner of the card
    }
  }
]

const rivalPlayers = usersIds.map(doc => ({
                          userId: doc.data.userId,
                          power: cards.filter(card => card.id === doc.id)[0].power
                      }));

console.log(rivalPlayers)