JSFiddle - React, Tailwind, and code Playground
by cmbuckley
HTML
<textarea id=builder rows=10>
111
222
333
444
555
666
777</textarea><br />
<button id=test>Click me</button>
<p id=output></p>
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(';');
// output is now '555,666;777'
$('#output').text(output);
}
// 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);
});
}
$('#test').click(dobuild);