I have this:
我有这个:
<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296> <:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850> <:sss:409342761662414848>
Convert it to this array: using .split(' ')
将其转换为该数组:使用.split(' ')
[ '<:cmd:409342761179938838>',
'<:nobot:409342761246916610>',
'<:haha:409342761272344578>',
'<:rrrr:409342761431728139>',
'<:aaa:409342761439854593>',
'<:fff:409342761503031296>',
'<:woah:409342761532391424>',
'<:swon:409342761549037568>',
'<:owoah:409342761595043850>',
'<:sss:409342761662414848>' ]
But i don't know how to split it several arrays so each will have 100 symbols in it
但是我不知道如何将它分割成几个数组所以每个数组都有100个符号
like this:
是这样的:
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578>' ],
[ '<:rrrr:409342761431728139> <:aaa:409342761439854593> <:fff:409342761503031296>' ],
[ '<:woah:409342761532391424> <:swon:409342761549037568> <:owoah:409342761595043850>' ],
[ '<:sss:409342761662414848>']
p.s. i tried use .match(/.{1,100}/g);
but i got this
p.s.我尝试使用.match(/.{1100}/g);但我得到这个
[ '<:cmd:409342761179938838> <:nobot:409342761246916610> <:haha:409342761272344578> <:rrrr:409342761431 '], ['728139>... etc ']
and i don't want to split <:rrrr:409342761431728139>
我不想拆分<:rrrr:409342761431728139>
3 个解决方案
#1
1
Besides the first suggested approach, there is a functional way, using the reduce
function:
除了第一个建议的方法外,还有一个功能的方法,使用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
1
Split the string on spaces. Then concatenate each string to a result until it would be over 100 characters long, then you push it onto the result array.
在空格上分割字符串。然后将每个字符串连接到一个结果,直到它超过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);
#3
0
@D.Lene, according to the output provided in the question. I have written the below code which gives youur output.
@D。Lene,根据问题中提供的输出。我已经写了下面的代码给你输出。
Note: Please do not forget to look at code executed on Node REPL after the below code. It will give you a clean understanding of each and every line.
注意:请不要忘记查看下面代码后在节点REPL上执行的代码。它会让你对每一行都有一个清晰的理解。
And leave comment if the below code does not satify your need by provide more outputs (that you actuially need for some specific inputs) as I am not counting for 100 characters as your provided output array's elements are not exactly of length 100 (it's 80, 78, 81, ...) which is clear in Node REPL testing.
下面留言,如果代码不满足你的需要,提供更多的输出(你actuially需要一些特定的输入)我不包括提供100个字符作为输出数组的元素不是长度为100(80,80,78,…)中明确节点REPL测试。
If you are still looking for 100 characters then based on your provided data input(s) & output(s), I will need to change the code.
如果您仍然在寻找100个字符,那么基于您提供的数据输入和输出,我将需要更改代码。
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>"
]
*/
Finally look at the below code executed on Node REPL to understand few of the above basic statements.
最后,查看在Node 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
>
Thanks.
谢谢。
#1
1
Besides the first suggested approach, there is a functional way, using the reduce
function:
除了第一个建议的方法外,还有一个功能的方法,使用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
1
Split the string on spaces. Then concatenate each string to a result until it would be over 100 characters long, then you push it onto the result array.
在空格上分割字符串。然后将每个字符串连接到一个结果,直到它超过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);
#3
0
@D.Lene, according to the output provided in the question. I have written the below code which gives youur output.
@D。Lene,根据问题中提供的输出。我已经写了下面的代码给你输出。
Note: Please do not forget to look at code executed on Node REPL after the below code. It will give you a clean understanding of each and every line.
注意:请不要忘记查看下面代码后在节点REPL上执行的代码。它会让你对每一行都有一个清晰的理解。
And leave comment if the below code does not satify your need by provide more outputs (that you actuially need for some specific inputs) as I am not counting for 100 characters as your provided output array's elements are not exactly of length 100 (it's 80, 78, 81, ...) which is clear in Node REPL testing.
下面留言,如果代码不满足你的需要,提供更多的输出(你actuially需要一些特定的输入)我不包括提供100个字符作为输出数组的元素不是长度为100(80,80,78,…)中明确节点REPL测试。
If you are still looking for 100 characters then based on your provided data input(s) & output(s), I will need to change the code.
如果您仍然在寻找100个字符,那么基于您提供的数据输入和输出,我将需要更改代码。
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>"
]
*/
Finally look at the below code executed on Node REPL to understand few of the above basic statements.
最后,查看在Node 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
>
Thanks.
谢谢。