首页 > 解决方案 > JavaScript 数组 reduce() 累加器为空

问题描述

我正在拆分一个单词并将每个单独的元素转换为莫尔斯电码。但是,返回值为空。不知道我在这里做错了什么。

        wordTransformed = word.split("").reduce((total, letter) => {
            currentLetterIndex = alphabet.indexOf(letter);
            total + morseAlphabet[currentLetterIndex];
            return total
        },"");

        console.log(wordTransformed);

标签: javascriptecmascript-6

解决方案


total + morseAlphabet[currentLetterIndex];

您正在寻找

total += morseAlphabet[currentLetterIndex];

https://www.w3schools.com/js/js_assignment.asp


推荐阅读