1 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊
您可以為字母及其數(shù)字獲取一個對象,并通過查找某個字母進(jìn)行轉(zhuǎn)換。
const
DIGIT_TO_LETTERS = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] },
LETTERS_TO_DIGITS = Object
.entries(DIGIT_TO_LETTERS)
.reduce((r, [v, letters]) => {
letters.forEach(l => r[l] = +v);
return r;
}, {}),
convert = string => Array.from(string, l => LETTERS_TO_DIGITS[l]);
console.log(convert('mad'))
添加回答
舉報(bào)