首页 > 解决方案 > 减少数组以从左到右删除重复的 matchId 条目

问题描述

当用户更新 matchId 的分数时,我将对象送到数组中,我希望我的最终数组删除 matchId 的所有最旧的重复项,所以从索引 0 到结尾,有办法吗?

我有这个:

cleanData =
    0: {matchId: "271691", homeTeamScore: "1", awayTeamScore: "1"}
    1: {matchId: "271692", homeTeamScore: "1", awayTeamScore: "1"}
    2: {matchId: "271700", homeTeamScore: "1", awayTeamScore: "1"}
    3: {matchId: "271691", homeTeamScore: "6", awayTeamScore: "6"}
    4: {matchId: "271691", homeTeamScore: "8", awayTeamScore: "8"}
    5: {matchId: "271691", homeTeamScore: "8", awayTeamScore: "8"}
    6: {matchId: "271691", homeTeamScore: "8", awayTeamScore: "0"}

我想要这个:

0: {matchId: "271692", homeTeamScore: "1", awayTeamScore: "1"}
1: {matchId: "271700", homeTeamScore: "1", awayTeamScore: "1"}
2: {matchId: "271691", homeTeamScore: "8", awayTeamScore: "0"}

我的代码:

saveResult(data: any, pushMatchId: any) {
    if (this.savedResults) {
      let cleanData = this.savedResults.map((item) => {
        return {
          matchId: item.matchId,
          homeTeamScore: item.homeTeamScore,
          awayTeamScore: item.homeTeamScore,
        };
      });
      data.map((item) => {
        cleanData.push(item);
      });
      this.db.collection("users").doc(this.user.uid).update({
        results: cleanData,
      });
    } else {
      this.db.collection("users").doc(this.user.uid).set({
        results: data,
      });
    }

  }

标签: javascripttypescriptangularfirereduce

解决方案


由于数组中的最新对象是您想要的对象,因此您可以使用reduceRight()并将尚未在最终数组中的匹配项添加到最终结果中,例如:

let matches = [
  { matchId: "271691", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271692", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271700", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271691", homeTeamScore: "6", awayTeamScore: "6" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "8" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "8" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "0" },
];

let result = matches.reduceRight(
  (p, c) => (!p.find((o) => o.matchId === c.matchId) ? [...p, c] : p),
  []
);

console.log(result);

或更优化的方法,将最新的 matchIds 存储在地图中:

let matches = [
  { matchId: "271691", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271692", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271700", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271691", homeTeamScore: "6", awayTeamScore: "6" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "8" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "8" },
  { matchId: "271691", homeTeamScore: "8", awayTeamScore: "0" },
];

let matchIds = new Map();
let result = matches.reduceRight((p, c) => {
  if (!matchIds.get(c.matchId)) {
    matchIds.set(c.matchId, true);
    return [...p, c];
  }
  return p;
}, []);

console.log(result);

如果您想事先检查是否已经存在与某个 id 匹配的匹配项并在添加最新匹配项之前将其过滤掉,您可以执行以下操作:

let matches = [
  { matchId: "271691", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271692", homeTeamScore: "1", awayTeamScore: "1" },
  { matchId: "271700", homeTeamScore: "1", awayTeamScore: "1" },
];

let addMatch = (match) => {
  matches = matches.filter(({
    matchId
  }) => matchId !== match.matchId);
  return matches.push(match), matches;
};

let latest = { matchId: "271691", homeTeamScore: "6", awayTeamScore: "6" };

addMatch(latest); // Add the latest match, replace if there was already a score

console.log(matches);


推荐阅读