我很困惑,试图解决这个问题。
1)我有一个PHP数组,它是JSON_ENCODE存储在JS数组1中。
2)然后是一个textarea,输入被分成一个JS array2。
3)然后比较两个阵列和Slice& Concat,其中包含从array1中扣除的值。
4)然后取array2并将其块化为50的arraySize。
我面临的问题是,我需要50块的块来分隔每个值的分号和另一个分块的末尾。
我以前使用过加入,但是';'被添加到数组中,我的arraySize搞砸了。
非常感谢任何帮助。
//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
存储在ci_sites中的值为:
111 222 333 444
存储在lines_arr中的值为:
111 222 333 444 555 666 777
输出将是:
555666777
chunk.join在for中,因为它将Chunks分解为50。
答案 0 :(得分:0)
以下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);
});
}
答案 1 :(得分:0)
我的解决方案:
//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();