You must do this:
var arr = str.toString().split(",");
"TypeError: Object data1, data2 does not have a split method, indicates that the variable is not considered as a string. Therefore, you should output it.
update 10/08/2015 I noticed that someone believes that the above answer is a "dirty workaround", and, surprisingly, this comment is supported. In fact, this is the exact opposite - using str[0].split(",") as 3 (!) Of the other sentences is a real "dirty workaround." What for? Think about what happens in these cases:
var str = []; var str = ['data1,data2','data3,data4']; var str = [someVariable];
str[0].split(",") will fail as soon as str contains an empty array, for some reason does not hold String.prototype or gives an unsatisfactory result if str contains more than one string. Using str[0].split(",") , blindly relying on the fact that str will always contain 1 string for sure and will never be bad practice. toString() supported by numbers, arrays, objects, Booleans, dates, and even functions; str[0].split() has enormous potential for raising errors and stopping further execution in the field, and thus the entire application may crash.
If you really want to use str[0].split() , then at least do a minimal check:
var arr; if (typeof str[0] == 'string') { arr = str[0].split(',') } else { arr = []; }
davidkonrad
source share