If this is a string, you can use the functions .split() and .join() , as well as .push() :
var data = '3434,346,1,6,46'; var arr = data.split(','); var add = newInt; arr.push(newInt); data = arr.join(',');
If it is already an array, you can simply use .push() :
var data = [3434,346,1,6,46]; var add = newInt; data.push(add);
UPDATE: did not read the last line to check for duplicates, the best approach I can think of is a loop:
var data = [3434,346,1,6,46]; var add = newInt; var exists = false; for (var i = 0; i < input.length; i++) { if (data[i] == add) { exists = true; break; } } if (!exists) { data.push(add);
Ribose
source share