1

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.

2) Then a textarea, which the input is being split into a JS array2.

3) then compare both arrays and Slice & Concat with values from array1 deducted.

4) then take array2 and chunk it into arraySize of 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.

I have previously used a join, but the ';' gets added to the array and my arraySize gets messed up.

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:

111 222 333 444

The Values stored in lines_arr are:

111 222 333 444 555 666 777

The ouput will be:

555,666,777

The chunk.join is in the for as it breaks the Chunks into 50.

  • Should'nt your chunk.join be outside the for ? Do you have an actual example with something into the json objects and what the output should look like ? – Sly Nov 19 '14 at 10:55
  • i build an example here : http://jsfiddle.net/nqruwfx9/ what doesn't do what you want in it ? – Sly Nov 19 '14 at 11:17
  • I am looking for the ',' to be replaced with ';' with one added on the end. I have added more values into that fiddle for "var text" when it chunks it into 50 I will need a ';' on the end of each chunk also. Thanks for the help. – timeismoney Nov 19 '14 at 11:26
  • You ar epushing an array into an array, so replace : chunk.push(cleaned.slice(i*arraySize,i*arraySize+arraySize) ); with : chunk.push(cleaned.slice(i*arraySize,i*arraySize+arraySize).join(';') ); – Sly Nov 19 '14 at 11:30
  • see http://jsfiddle.net/nqruwfx9/1/ – Sly Nov 19 '14 at 11:31

2 Answers2

0

The following JavaScript should meet your needs:

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);
    });
}

JSFiddle example

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

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

demo here

Sly
  • 361
  • 2
  • 9