9

My array:

var str=['data1,data2 '];

I have used:

var arr = str.split(",");

But one error is showed. TypeError: Object data1,data2 has no method 'split'. How can I solve this problem.

My output will be:

arr= data1,data2
// or
arr[0]=data1;
arr[1]=data2;

How can I solve this problem ?

0

4 Answers 4

15

You should do this :

var arr = str.toString().split(",");

"TypeError: Object data1,data2 has no method 'split'" indicates the variable is not considered as a string. Therefore, you must typecast it.


update 08.10.2015 I have noticed someone think the above answer is a "dirty workaround" and surprisingly this comment is upvoted. In fact it is the exact opposite - using str[0].split(",") as 3 (!) other suggests is the real "dirty workaround". Why? Consider what would happen in these cases :

var str = [];
var str = ['data1,data2','data3,data4'];
var str = [someVariable]; 

str[0].split(",") will fail utterly as soon str holds an empty array, for some reason not is holding a String.prototype or will give an unsatisfactory result if str holds more than one string. Using str[0].split(",") blindly trusting that str always will hold 1 string exactly and never something else is bad practice. toString() is supported by numbers, arrays, objects, booleans, dates and even functions; str[0].split() has a huge potential of raising errors and stop further execution in the scope, and by that crashing the entire application.

If you really, really want to use str[0].split() then at least do some minimal type checking :

var arr;
if (typeof str[0] == 'string') {
    arr = str[0].split(',')
} else {
    arr = [];
}
3
  • Still, you count on the .toString() implementation of the array to always use , as its own delimeter. That is not set in stone. What if some future browser will decide to use newline character instead? Oct 8, 2015 at 7:54
  • 1
    I mean that you assume ['data1,data2','data3,data4'].toString() will return 'data1,data2,data3,data4' - I don't think it's set in stone. Best approach is joining it yourself i.e. ['data1,data2','data3,data4'].join(',') Oct 8, 2015 at 8:07
  • @ShadowWizard, Yes! Now I see what you mean, refering to str = ['data1,data2','data3,data4'] - you are absolutely right! We cannot be sure browsers or V8 in this case will return data1,data2,data3,data4 in all future. Oct 8, 2015 at 8:11
5

If your starting point is a array with a string inside. This should work:

var arr = str[0].split(",");

Otherwise you should have a string as starting point for your code to work as you expected:

var str = 'data1,data2';

If you have more elements in the array you will need to iterate them with a for loop.

Edit to add other cases:

If you have several strings in that array, then you should be more carefull and do something like this:

var str = ['data1,data2 ', ' data3, data4 ']; // notice these strings have many spaces in different places
var longString = str.join(',');
var array = longString.split(',').map(s => s.trim()).filter(Boolean); // removing spaces from start and end of strings, and eventually removing empty positions

console.log(array);

3
  • 1
    this should be the chosen answer. the current winner is just a dirty workaround that doesn't teach anything. Mar 26, 2015 at 0:46
  • 2
    @Jonathan - I do not consider Array.toString() as a "dirty workaround" at all. In fact it is the safe, secure way to do it. I think it is rather obvious why str[0].split(",") has the potential to crash the entire application. Have updated my answer to explain why. Oct 8, 2015 at 6:41
  • 1
    @davidkonrad added a edit, to include cases of multiple strings inside that array.
    – Sergio
    Dec 20, 2016 at 10:21
3

As you said, str is an array (with one element). If you want to split the string contained in the array, you have to access the array first:

var arr = str[0].split(",");
-1

let's say we have two date :

date1: 17/01/1989

date2: 20/02/2000

if we want to compare them just split the string and compare like this

var date1= date1.toString().split("/");

var date2= date2.toString().split("/");

var a = parseInt(date1[2] + date1[1] + date1[0]);

var b = parseInt(date2[2] + date2[1] + date2[0]);

if(a < b){

alert("date2 bigger than date1")}

} else if(a > b){

alert("date1 bigger than date2")

} else{

alert("date 1 and date2 are equals ");

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.