【问题标题】:Conditionally split and concat text有条件地拆分和连接文本
【发布时间】:2020-01-29 16:32:21
【问题描述】:

我正在尝试有条件地拆分数组中的每个字符串。这是我的数组。

const categories = [
  "Department of Natural Science",
  "Department of public health and sanitation",
  "Department of culture and heritage of state"
];

再次通过拆分每个字符串,我想将其更改为数组。该数组包含几个字符串块。例如。通过拆分Department of culture and heritage of state 字符串,我希望将其分开Department of Natural Science。在这里,我想创建每个不同的块如果块包含超过 13 个字符。这就是 NaturalScience 分开的原因,因为如果我们将它们的长度相加,它就会变成 14

这是我尝试过的。

const categories = [
  "Department of Natural Science",
  "Department of public health and sanitation",
  "Department of culture and heritage of state"
];

const arrayOfAllString = []; // results at the end

categories.map(cur => {
  // looping the array
  const splitedItems = cur.trim().split(" "); // splitting the current string into words
  const arrayOfSingleString = []; //
  let str = "";
  splitedItems.map(item => {
    // looping the array of splitted words
    if (str.length + item.length > 13) {
      // trying to make a chunk
      arrayOfSingleString.push(str);
      str = ""; // clearing the str because it has been pushed to arrayOfSingleString
    } else {
      str = str.concat(item + " "); // concat the str with curent word
    }
  });
  arrayOfAllString.push(arrayOfSingleString);
});

console.log(arrayOfAllString);

我的预期结果会是这样的:

arrayOfAllString = [
  ["Department of", "Natural", "Science"],
  ["Department of", "public health", "and", "sanitation"],
  ["Department of", "culture and", "heritage of", "state"]
];

【问题讨论】:

  • "字符长度超过 13 个单词"。您的所有字符串都不包含超过 13 个单词。你能说出你真正的意思吗?

标签: javascript arrays split concat


【解决方案1】:

您可以使用生成器并返回所需长度的块。

function* getJoined(string, size) {
    var array = string.split(' '),
        i = 0;

    while (i < array.length) {
        let s = array[i];
        while (++i < array.length && (s + array[i]).length < size) {
            s += ' ' + array[i];
        }
        yield s;
    }
}

console.log([...getJoined('Department of culture and heritage of state', 13)]);

经典方法不会误用map

function getJoined(string) {
    var array = string.split(' '),
        size = 13,
        i = 0,
        result = [];

    while (i < array.length) {
        let s = array[i];
        while (++i < array.length && (s + array[i]).length < size) {
            s += ' ' + array[i];
        }
        result.push(s);
    }
    return result;
}

const categories = ["Department of Natural Science", "Department of public health and sanitation", "Department of culture and heritage of state"];

console.log(categories.map(getJoined));
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 哇。谢谢。函数*在做什么?以及该地图功能如何在不这样做的情况下工作 .map(cur => getJoined(cur)) ??
  • 查看function*Array#mapmap 如果函数与映射所需的回调具有相同的签名,则直接使用该函数。
【解决方案2】:

做了一些改动。 1)清除时,更改为str = item;而不是str = '' 2) 循环结束,arrayOfSingleString.push(str); 添加最后一项。

const categories = [
  "Department of Natural Science",
  "Department of public health and sanitation",
  "Department of culture and heritage of state"
];

const arrayOfAllString = categories.map(cur => {
  const splitedItems = cur.trim().split(" ");

  const arrayOfSingleString = [];
  let str = "";
  while (splitedItems.length > 0) {
    const item = splitedItems.shift();
    if (str.length + item.length >= 13) {
      // trying to make a chunk
      arrayOfSingleString.push(str);
      // clearing the str because it has been pushed to arrayOfSingleString
      str = item;
    } else {
      // concat the str with curent word
      str = str ? `${str} ${item}` : item;
    }
  }
  arrayOfSingleString.push(str);
  return arrayOfSingleString;
});

console.log(arrayOfAllString);

【讨论】:

  • 为什么文字之间没有空格?
  • @SandeshSapkota,我的错,我解决了问题并更新了答案。
猜你喜欢
  • 1970-01-01
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 2011-02-26
  • 2018-07-18
相关资源
最近更新 更多