2

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(' ')

[ '<: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

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

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

and i don't want to split <:rrrr:409342761431728139>

5
  • What do you want here? Do you want exactly 100 characters? Well, that's probably not possible. Do you want less/more than 100 characters, but as close as you can get? Something else? Commented May 22, 2018 at 1:26
  • @TimBiegeleisen yes i want to get as close as i can to 100 Commented May 22, 2018 at 1:28
  • Start with your original .split() array. Loop through the array, concatating elements to the strings in the result array. Whenever the length gets above 100 start a new string. Commented May 22, 2018 at 1:34
  • Do you really need each string to be in its own nested array? Or just one array of these strings. Commented May 22, 2018 at 1:34
  • @Barmar one array splitted with ',' will work as well Commented May 22, 2018 at 1:38

3 Answers 3

1

Besides the first suggested approach, there is a functional way, using the reduce function:

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

  }, []);
Sign up to request clarification or add additional context in comments.

1 Comment

Good alternative. Thank you!
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.

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 Comments

I think instead of minimum 100 chars, OP is asking for nearest char closest to 100 but not exceeding 100. That's why the first element in the array is 80
@D.Lene My code was making strings >100 characters long. I changed it to push just before it gets to 100, so it produces the same result you wanted.
@Isaac I realized that and changed it.
0

@D.Lene, according to the output provided in the question. I have written the below code which gives youur output.

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.

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.

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.

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.