[英]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:所以这些是规则:
<!--startline-->[new text="table" ids="
+ chunk
+ "]
<!--startline-->[new text="table" ids="
+ chunk
+ "]
<!--startline-->[new text="table" files="2" ids="
+ chunk
+ "]
<!--startline-->[new text="table" files="2" ids="
+ chunk
+ "]
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 提供解决方案的第一部分!
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.