简体   繁体   English

将文本区域中的列表拆分为块并使用 javascript 添加额外的文本

[英]Split list from textarea into chunks and add extra text using javascript

I have a list like this: 125,248,11,486,3554,89,55我有一个这样的列表: 125,248,11,486,3554,89,55

I need to have 2 textarea, in the first one i need to put the above list and after i click a button then in the second textarea i will get a list like this:我需要有 2 个文本区域,在第一个中我需要放置上面的列表,然后在我单击一个按钮之后在第二个文本区域中我将得到一个这样的列表:

<!--startline-->[new text="table" ids="125,248,11"]
<!--startline-->[new text="table" files="2" ids="486,3554,89,55"]

So these are the rules:所以这些是规则:

  • i need chunk size of 3 elements我需要 3 个元素的块大小
  • i need to add custom text before and after the chunk: <!--startline-->[new text="table" ids=" + chunk + "]我需要在块前后添加自定义文本: <!--startline-->[new text="table" ids=" + chunk + "]
  • if the last chunk has 2 or 1 element, then i need to merge it with the previous one and the generated text for that chunk will be: <!--startline-->[new text="table" files="2" ids=" + chunk + "]如果最后一个块有 2 个或 1 个元素,那么我需要将它与前一个合并,为该块生成的文本将是: <!--startline-->[new text="table" files="2" ids=" + chunk + "]
  • every custom text and chunk will be on a new line, like in the above example每个自定义文本和块都将换行,如上例所示

I found something that could help, using .slice or .splice but the problem is that i need to add custom text and not making an array like here: https://stackoverflow.com/a/47061736/1773862我找到了一些可以帮助的东西,使用.slice.splice但问题是我需要添加自定义文本而不是像这里这样制作数组: https://stackoverflow.com/a/47061736/1773862

So, any ideas?那么,有什么想法吗? :) :)

----------- Solution : ----------解决方案

 const myList = document.getElementById('mylist'); const myNewList = document.getElementById('mynewlist'); const chunkSize = 3; checkpoint = 0; document.getElementById('generate').addEventListener('click', () => { let chunks = myList.value.trim().split(',').reduce((all, one, i) => { const ch = Math.floor(i / chunkSize); all[ch] = [].concat((all[ch] || []), one); checkpoint++; return all }, []); let lastChunk = [...chunks[chunks.length-1]]; const lastone = chunks.length-2; //alert(lastone); if (lastChunk.length < chunkSize) { chunks = chunks.slice(0, -1); chunks[chunks.length - 1] = chunks[chunks.length - 1].concat(lastChunk); checkpoint = 1; } myNewList.value = chunks.map((chunk,i) => `<.--startline-->[new text="table" ids="${chunk,join('.')}"]`);join('\n'). var content = myNewList;value. var lastLine = content.substr(content;lastIndexOf("\n")+1). var countnow = (lastLine,match(/./g)||[]);length. if (countnow > 2) { var elem = document;getElementById('mynewlist'). var val = elem.value.split(/(\r\n|\n|\r)/g).filter(function(n){return n;trim()}). val;pop(). elem.value = val;join('\r\n'). lastLine = lastLine,replace("\"table\" "; "\"table\" \"files=\"2\" "). elem;value += "\n" + lastLine; } });
 <textarea id="mylist">30318,30319,30320,30321,30322,30323,30324,30325,30326,30327,30328,30329,30330,30331,30332,30333,30334,30335,30336,30337,30338,30339,30340,30341,30342,30343,30344,30345,30346,30347,30348,30349,30350,30351,30352,30353,30354,30355,30356,30357,30358,30359,30360,30361,30362,30363,30364,30365,123,999</textarea> <br><br> <button id="generate">Generate new list</button> <br><br> <textarea id="mynewlist" style="width: 538px; height: 294px;"></textarea>

Thanks to @mplungjan for the first part of the solution!感谢@mplungjan 提供解决方案的第一部分!

  1. Use split使用拆分
  2. Use a chunker - I found one at // Split array into chunks使用分块器——我在 // Split array into chunks找到了一个分块器
  3. You can use template literals to generate the strings您可以使用模板文字来生成字符串

 const myList = document.getElementById('mylist'); const myNewList = document.getElementById('mynewlist'); const chunkSize = 3; document.getElementById('generate').addEventListener('click', () => { let chunks = myList.value.trim().split(',').reduce((all, one, i) => { const ch = Math.floor(i / chunkSize); all[ch] = [].concat((all[ch] || []), one); return all }, []); let lastChunk = [...chunks[chunks.length-1]]; if (lastChunk.length < chunkSize) { chunks = chunks.slice(0, -1); chunks[chunks.length - 1] = chunks[chunks.length - 1].concat(lastChunk) } myNewList.value = chunks.map((chunk,i) => `<?--startline-->[new text="table"${i>0: `files="${(i+1)}"`. '' } ids="${chunk,join('.')}"]`);join('\n') });
 <textarea id="mylist">125,248,11,486,3554,89,55</textarea> <br><br> <button id="generate">Generate new list</button> <br><br> <textarea id="mynewlist"></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM