在具有一定长度的n个数组中拆分数组

时间:2018-05-22 01:24:44

标签: javascript arrays node.js

我有这个:

<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296> <:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850> <:sss:409342761662414848>

将其转换为此数组: 使用.split('')

[ '<:cmd:409342761179938838>',
  '<:nobot:409342761246916610>',
  '<:haha:409342761272344578>',
  '<:rrrr:409342761431728139>',
  '<:aaa:409342761439854593>',
  '<:fff:409342761503031296>',
  '<:woah:409342761532391424>',
  '<:swon:409342761549037568>',
  '<:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]

但是我不知道如何将它分成几个数组,因此每个数组中都有100个符号

像这样:

[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>' ],
[ '<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>' ],
[ '<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>' ],
[ '<:sss:409342761662414848>']

P.S。我试过用.match(/.{1,100}/g); 但我得到了这个

[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431 '], ['728139>... etc ']

我不想拆分<:rrrr:409342761431728139>

3 个答案:

答案 0 :(得分:1)

在空格上拆分字符串。然后将每个字符串连接到一个结果,直到它超过100个字符,然后将其推送到结果数组。

var str = '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296> <:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850> <:sss:409342761662414848>';
var array = str.split(' ');
var result = [];
var curstr = '';
array.forEach(s => {
  var newstr = curstr + (curstr != '' ? ' ' : '') + s;
  if (newstr.length > 100) {
    result.push(curstr);
    curstr = s;
  } else {
    curstr = newstr;
  }
});
if (curstr != '') { // Get the last string, which wouldn't have been pushed in the loop
  result.push(curstr);
}
console.log(result);

答案 1 :(得分:1)

除了第一个建议的方法,还有一种功能方式,使用reduce函数:

const arr = str.split(' ')
  .reduce((acc, item) => {

    const idx = acc.length ? acc.length - 1 : 0

    const tempStr = `${acc[idx] || ''} ${item}`;

    return tempStr.length <= 100
      ? Object.assign(acc, { [idx]: tempStr })
      : [...acc, item];

  }, []);

答案 2 :(得分:0)

根据问题中提供的输出,

@ D.Lene 。我写了下面的代码,它给你的输出。

  

注意:请不要忘记查看以下代码后在节点REPL 上执行的代码。它将让您清楚地了解每一行。

     

如果以下代码不能满足您的需求,请提供更多输出(您原本需要某些特定输入),因为我不计算100个字符,因为您提供的输出数组的元素不完全是100(它是80,78,81,......)在节点REPL 测试中很清楚。

     

如果您仍在寻找100个字符,那么请根据您提供的数据输入&amp;输出,我需要更改代码。

var data = "<:cmd:409342761179938838> " +
          "<:nobot:409342761246916610> " +
          "<:haha:409342761272344578> " +
          "<:rrrr:409342761431728139> " +
          "<:aaa:409342761439854593> " +
          "<:fff:409342761503031296> " +
          "<:woah:409342761532391424> " +
          "<:swon:409342761549037568> " +
          "<:owoah:409342761595043850> "+
          "<:sss:409342761662414848>"

var arr = data.split(' ')
console.log(arr)
/*
  [ '<:cmd:409342761179938838>',
    '<:nobot:409342761246916610>',
    '<:haha:409342761272344578>',
    '<:rrrr:409342761431728139>',
    '<:aaa:409342761439854593>',
    '<:fff:409342761503031296>',
    '<:woah:409342761532391424>',
    '<:swon:409342761549037568>',
    '<:owoah:409342761595043850>',
    '<:sss:409342761662414848>' ]
*/

var mainArr = []

while(arr.length) {
      mainArr.push(arr.splice(0, 3).join(' '))
}

// Pretty printing array 
// (This is the o/p which is specified in the question, here I forgot everything about 100 characters)
console.log(JSON.stringify(mainArr, null, 4))
/*
  [
      "<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>",
      "<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>",
      "<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>",
      "<:sss:409342761662414848>"
  ]
*/

最后看一下在节点REPL 上执行的以下代码,以了解上述几个基本陈述。

H:\RishikeshAgrawani\Projects\Stk>node
>
> var data = "<:cmd:409342761179938838> " +
...           "<:nobot:409342761246916610> " +
...           "<:haha:409342761272344578> " +
...           "<:rrrr:409342761431728139> " +
...           "<:aaa:409342761439854593> " +
...           "<:fff:409342761503031296> " +
...           "<:woah:409342761532391424> " +
...           "<:swon:409342761549037568> " +
...           "<:owoah:409342761595043850> "+
...           "<:sss:409342761662414848>"
undefined
>
> arr
[ '<:cmd:409342761179938838>',
  '<:nobot:409342761246916610>',
  '<:haha:409342761272344578>',
  '<:rrrr:409342761431728139>',
  '<:aaa:409342761439854593>',
  '<:fff:409342761503031296>',
  '<:woah:409342761532391424>',
  '<:swon:409342761549037568>',
  '<:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]
>
> arr[0]
'<:cmd:409342761179938838>'
> arr[0].length
25
>
> arr[1].length
27
>
> arr[2].length
26
>
> arr[3].length
26
> arr[4].length
25
> arr[5].length
25
> arr[6].length
26
>
> var mainArr = []
undefined
>
> while(arr.length) {
...       mainArr.push(arr.splice(0, 3).join(' '))
... }
4
>
> mainArr
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>',
  '<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>',
  '<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>',
  '<:sss:409342761662414848>' ]
>
> mainArr[0]
'<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>'
>
> mainArr[1]
'<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>'
>
> mainArr[2]
'<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>'
>
> mainArr[0].length
80
> mainArr[1].length
78
> mainArr[2].length
81
>

感谢。