有条件地拆分和连接文本

Conditionally split and concat text

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

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"]
];

您可以使用生成器和 return 所需长度的块。

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; }

做了一些改动。 1) 清除时,将 str = '' 改为 str = item; 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);