Typescript reduce 我想对数字求和,但之前的值变成了字符串
Typescript reduce I want to sum numbers but the previous value becomes string
我有一个打字稿函数,它遍历一个对象数组,并对它们进行汇总。
如果我隐式给值一个数字,它就可以工作,但是如果它来自输入,它会将值转换为字符串。
const mmrChanges = matches.reduce<IHistory[]>(
(previous: IHistory[], match: IMatch, index: number) => {
const mmrChange = (match.party_size > 1 ? 20 : 30) * isWon(match); // party game 20 mmr, solo game 30
const prevMmr = previous[previous.length - 1].value;
return [
...previous,
{
index: index,
value: prevMmr + mmrChange,
time: new Date(match.start_time * 1000).toISOString().split("T")[0],
},
];
},
[{ index: 0, value: currentMmr, time: "" }]
);
console.log(mmrChanges);
值类型是:(属性) IHistory.value: 数字
当前的 mmr 是:const currentMmr: number
但是,当我开始累积值数字时,它开始将值连接为字符串。
当前值 = 400
下一次迭代变成:
{
"index": 0,
"value": "40030",
"time": "2022-01-25"
}
我试图强制它在 reduce 函数中使用“as number”明确编号
我还尝试了 parseInt() 函数,但随后打字稿抛出错误我无法将数字转换为 int.
我在这里错过了什么?
如果我将 currentMmr 更改为 0,它会按预期工作。
下面的解决方案似乎达到了预期的效果:
const mmrChanges = matches.reduce<IHistory[]>(
(previous: IHistory[], match: IMatch, index: number) => {
const mmrChange = (match.party_size > 1 ? 20 : 30) * isWon(match); // party game 20 mmr, solo game 30
const prevMmr = previous[previous.length - 1].value;
return [
...previous,
{
index: index,
value: prevMmr + mmrChange,
time: new Date(match.start_time * 1000).toISOString().split("T")[0],
},
];
},
[{ index: 0, value: parseInt(currentMmr.toString()) || 0, time: "" }]
);
console.log(mmrChanges);
发生了什么变化?
.reduce
聚合器设置为 [{ index: 0, value: parseInt(currentMmr.toString()) || 0, time: "" }]
为什么?
题目中,currentMmr
可以是任意值。为了确保它始终是一个 int,value
prop 被设置为 parseInt(currentMmr.toString())
。 .toString()
确保在 currentMmr
是整数的情况下,它仍被视为字符串。并且,parseInt
将字符串转换为 int。
注意:这个解决方案迎合了这个特定的问题。它可能无法在其他情况下按原样工作,可能需要进行自定义。
我有一个打字稿函数,它遍历一个对象数组,并对它们进行汇总。 如果我隐式给值一个数字,它就可以工作,但是如果它来自输入,它会将值转换为字符串。
const mmrChanges = matches.reduce<IHistory[]>(
(previous: IHistory[], match: IMatch, index: number) => {
const mmrChange = (match.party_size > 1 ? 20 : 30) * isWon(match); // party game 20 mmr, solo game 30
const prevMmr = previous[previous.length - 1].value;
return [
...previous,
{
index: index,
value: prevMmr + mmrChange,
time: new Date(match.start_time * 1000).toISOString().split("T")[0],
},
];
},
[{ index: 0, value: currentMmr, time: "" }]
);
console.log(mmrChanges);
值类型是:(属性) IHistory.value: 数字 当前的 mmr 是:const currentMmr: number
但是,当我开始累积值数字时,它开始将值连接为字符串。 当前值 = 400
下一次迭代变成:
{
"index": 0,
"value": "40030",
"time": "2022-01-25"
}
我试图强制它在 reduce 函数中使用“as number”明确编号 我还尝试了 parseInt() 函数,但随后打字稿抛出错误我无法将数字转换为 int.
我在这里错过了什么? 如果我将 currentMmr 更改为 0,它会按预期工作。
下面的解决方案似乎达到了预期的效果:
const mmrChanges = matches.reduce<IHistory[]>(
(previous: IHistory[], match: IMatch, index: number) => {
const mmrChange = (match.party_size > 1 ? 20 : 30) * isWon(match); // party game 20 mmr, solo game 30
const prevMmr = previous[previous.length - 1].value;
return [
...previous,
{
index: index,
value: prevMmr + mmrChange,
time: new Date(match.start_time * 1000).toISOString().split("T")[0],
},
];
},
[{ index: 0, value: parseInt(currentMmr.toString()) || 0, time: "" }]
);
console.log(mmrChanges);
发生了什么变化?
.reduce
聚合器设置为 [{ index: 0, value: parseInt(currentMmr.toString()) || 0, time: "" }]
为什么?
题目中,currentMmr
可以是任意值。为了确保它始终是一个 int,value
prop 被设置为 parseInt(currentMmr.toString())
。 .toString()
确保在 currentMmr
是整数的情况下,它仍被视为字符串。并且,parseInt
将字符串转换为 int。
注意:这个解决方案迎合了这个特定的问题。它可能无法在其他情况下按原样工作,可能需要进行自定义。