[英]javascript output as semicolon in chunks of 50
I am puzzled trying to work this one out. 我很困惑试图解决这一问题。
1) I have a PHP array which is JSON_ENCODE to store in a JS array1. 1)我有一个JSON_ENCODE的PHP数组要存储在JS array1中。
2) Then a textarea, which the input is being split into a JS array2. 2)然后是一个文本区域,其输入被拆分为一个JS数组2。
3) then compare both arrays and Slice & Concat with values from array1 deducted. 3)然后将两个数组以及Slice&Concat与从array1中扣除的值进行比较。
4) then take array2 and chunk it into arraySize of 50. 4)然后采用array2并将其分块为arraySize为50。
The issue I am facing is that I need the chunks of 50 to have a Semicolon seperating each value and another on the end of each chunk. 我面临的问题是,我需要50个块,以分号分隔每个值,并在每个块的末尾分隔另一个值。
I have previously used a join, but the ';' 我以前使用过联接,但是';' gets added to the array and my arraySize gets messed up. 被添加到数组,我的arraySize被弄乱了。
Any help is much appreciated. 任何帮助深表感谢。
//Store PHP values.
var ci_sites = <?echo json_encode($ci_pass);?>;
function dobuild(){
//Store textarea input into Array, Duplicates Removed.
var text = $("textarea#builder").val();
var lines = text.split(/\r\n|\s+\n|\s+\r|\n+|\r+/g);
var lines_arr = [];
$.each(lines,function(index, item){
if ($.inArray(item, lines_arr) ==-1)
lines_arr.push(item);
})
//Remove value from ci_sites.
var A1 = lines_arr;
var A2 = ci_sites;
for (var i = 0; i<A2.length; i++) {
var arrlen = A1.length;
for (var j = 0; j<arrlen; j++) {
if (A2[i] == A1[j]) {
A1 = A1.slice(0, j).concat(A1.slice(j+1, arrlen));
}
}
}
//Chunk The Array Into Sets Of 50.
var cleaned = A1;
var chunk = [];
var arraySize = 50;
for (var i = 0; i < Math.ceil(cleaned.length/arraySize); i++) {
chunk.push(cleaned.slice(i*arraySize,i*arraySize+arraySize));
var chunkbr = chunk.join("<br>");
$("#cleanlist").html("Cleaned CI's:<br><span style='color:#f00'>"+chunkbr+"</span>");
}*/
}`
@sly @狡猾
The Values stored in ci_sites are: 存储在ci_sites中的值是:
111 222 333 444 111222333444
The Values stored in lines_arr are: 存储在lines_arr中的值是:
111 222 333 444 555 666 777 111222333444555666777
The ouput will be: 输出将是:
555,666,777 555666777
The chunk.join is in the for as it breaks the Chunks into 50. chunk.join位于for中,因为它将块分成50个。
The following JavaScript should meet your needs: 以下JavaScript应该可以满足您的需求:
var ci_sites = ['111', '222', '333', '444'];
function dobuild() {
// grab the user input from the text area (unique)
var input = $('#builder').val()
.split(/\r\n|\s+\n|\s+\r|\n+|\r+/g)
.filter(function (value, index, self) {
return (self.indexOf(value) === index);
});
// diff between user and PHP content
var diff = input.filter(function (item) {
return (ci_sites.indexOf(item) < 0);
});
// chunk up the array (using chunks of 2 as an example)
var chunked = chunk(diff, 2);
// join them back
var output = chunked.map(function (item) {
return item.join(',');
}).join(';');
}
// http://stackoverflow.com/a/22649021/283078
function chunk(arr, n) {
return arr.slice(0, (arr.length + n - 1) / n | 0).map(function (c, i) {
return arr.slice(n * i, n * i + n);
});
}
my solution : 我的解决方案:
//Store PHP values.
var ci_sites = [111, 222, 333, 444];
function dobuild(){
//Store textarea input into Array, Duplicates Removed.
var text = '111 222 333 444 555 666 777 abc kjl poi sdf tyu pom bgf yui sdf uyt qdf etr hgf jkh sdg por jkh cdf cdf ùpo eri'.replace(/ /g,'\r\n'); //$("textarea#builder").val();
var lines = text.split(/\r\n|\s+\n|\s+\r|\n+|\r+/g);
var lines_arr = [];
$.each(lines,function(index, item){
if ($.inArray(item, lines_arr) ==-1)
lines_arr.push(item);
});
console.dir( lines_arr);
//Remove value from ci_sites.
var A1 = lines_arr;
var A2 = ci_sites;
for (var i = 0; i<A2.length; i++) {
var arrlen = A1.length;
for (var j = 0; j<arrlen; j++) {
if (A2[i] == A1[j]) {
A1 = A1.slice(0, j).concat(A1.slice(j+1, arrlen));
}
}
}
//Chunk The Array Into Sets Of 50.
var cleaned = A1;
var chunk = [];
var arraySize = 50;
for (var i = 0; i < Math.ceil(cleaned.length/arraySize); i++) {
chunk.push(cleaned.slice(i*arraySize,i*arraySize+arraySize).join(';') );
var chunkbr = chunk.join("<br>");
$("#cleanlist").append("<p>i=" + i + " Cleaned CI's:<br><span style='color:#f00'>"+chunkbr+"</span></p>");
}
}
dobuild();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.