javascript - 三个或更多字母的字母组合
问题描述
我想删除单个字符串并希望保留最小长度为 3 及以上的字符串我试图用这个 if(result.string >= 3) 访问字符串,但它给出了数组长度,所以我试图访问字符串,但我不能。所以请任何可以帮助我的人
let stringCombinations = (str) => {
let strLength = str.length;
let result = [];
let currentIndex = 0;
while (currentIndex < strLength) {
let char = str.charAt(currentIndex);
let x;
let arrTemp = [char];
for (x in result) {
arrTemp.push("" + result[x] + char);
}
result = result.concat(arrTemp);
currentIndex++;
}
return result;
};
console.log(stringCombinations("BLADE"));
This is my ouput
output: (31) ["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
This is what I want
["BLA", "BLD", "BAD", "LAD", "BLAD", "BLE", "BAE", "LAE", "BLAE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
解决方案
You want to filter your array using the string length:
["B", "L", "BL", "A", "BA", "LA", "BLA", "D", "BD", "LD", "BLD", "AD", "BAD", "LAD", "BLAD", "E", "BE", "LE", "BLE", "AE", "BAE", "LAE", "BLAE", "DE", "BDE", "LDE", "BLDE", "ADE", "BADE", "LADE", "BLADE"]
.filter(s => s.length >= 3)
.forEach(s => console.log(s))
推荐阅读
- function - Shell 脚本因函数语法错误而失败
- search - 在 Prolog 中获取节点的子节点列表
- javascript - 控制器和 HTML 之间的 AngularJs ng-model 延迟
- asp.net-core-2.0 - 访问令牌和刷新令牌过期时如何重定向到登录页面
- php - 尝试通过使用 AJAX PHP 切换开关来更新 Mysql
- python - Python显示没有这样的文件或目录错误,尽管文件存在
- react-native - 如何在 react-native 的键盘顶部显示自定义视图
- spring - 如何根据不同的工作环境提供不同的事务“超时”值
- java - 将 Android 的 JVM 更改为支持 LLVM/C 的编译器以平滑 android 之类的 ios
- java - Java Maven 无法找到或加载主类