1 回答
TA贡献1998条经验 获得超6个赞
首先,我建议dict在你的函数之外设置:
const dict = {
1: "١",
2: "٢",
3: "٣",
4: "٤",
5: "٥",
6: "٦",
7: "٧",
8: "٨",
9: "٩"
}
然后,正如已经说过的,您应该split将字符串转换为字符以获取它们的数组,然后您可以遍历该数组。之后,您应该将所有结果收集到一个带有join. 一个函数就像下一个一样简单:
const convertNumber = number => {
return number.toString().split("").map(digit => dict[digit]).join('');
};
最后,我建议使用它regular expressions来检查number它是否应该是。
const dict = {
1: "١",
2: "٢",
3: "٣",
4: "٤",
5: "٥",
6: "٦",
7: "٧",
8: "٨",
9: "٩"
}
const convertNumber = number => {
return number.toString().split("").map(digit => dict[digit]).join('');
};
const someNumbers = [123, 345, 334, 885];
const getNumberObject = arr => {
return arr.reduce((acc, cur) => {
acc[cur] = convertNumber(cur)
return acc;
}, {})
}
const numbers = getNumberObject(someNumbers)
console.log(numbers)
添加回答
举报