有条件地拆分和合并文本 [英] Conditionally split and concat text

查看:63
本文介绍了有条件地拆分和合并文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to conditionally split each of the string in an array.This is my array.

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

再次拆分每个字符串,我想将其更改为数组.该数组包含字符串的几个块.例如.通过拆分文化和国家遗产部门字符串,我希望这可以将 自然部门 科学部门分开.如果块的长度超过13个字符,我想在这里创建每个不同的块.这就是为什么 Natural Science 分开的原因,因为如果我们将它们的长度总和变为 14 .

Again by splitting each string I want to change it to an array. This array contains several chunk of the string. For eg. by splitting Department of culture and heritage of state string I want this to separate Department of Natural Science. Here I want to create every different chunk if the chunk contains more than 13 character in length. That's why Natural and Science separated because if we sum of the length of them it becomes 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);

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

My expected result would be somehow look like this :

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

推荐答案

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

You could take a generator and return chunks in the wanted length.

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

这篇关于有条件地拆分和合并文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆