javascript - 减少数组以从左到右删除重复的 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,
});
}
}
解决方案
由于数组中的最新对象是您想要的对象,因此您可以使用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);
推荐阅读
- android - 在Android中单击GoogleMap.setOnMarkerClickListener时获取值为null
- c# - 当我尝试使用它时,打开文件对话框会冻结程序
- python - 使用 scikit-learn 的 Gaussian Process Regression 时如何使用点的经纬度?
- javascript - 按照另一个数组给出的顺序,按字符串对对象数组进行排序
- react-native - 应用发布到 Expo 而其他人无法访问时,如何解决问题?
- apache - 将 Apache ssl 端口 443 转发到 Tomcat http 端口
- kotlin - 带有 let - 未解析引用的函数类型:invoke
- plot - 您如何在 MQL4 中像 Tradingview 中的 plot() 函数那样画一条线?
- javascript - 使用 MarvinJ JavaScript 进行边缘检测
- node.js - electronjs - 远程访问 nodejs