首页 > 解决方案 > 三个或更多字母的字母组合

问题描述

我想删除单个字符串并希望保留最小长度为 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"]

标签: javascriptarraysstringrandomword

解决方案


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))


推荐阅读