How to split a string on Node Js?

My array:

var str=['data1,data2 ']; 

I used:

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

But one mistake is shown. TypeError: Object data1, data2 does not have a split method. How can I solve this problem.

My conclusion will be:

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

How can I solve this problem?

+7
javascript split
source share
4 answers

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 = []; } 
+11
source share

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

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

Otherwise, you should have a string as a 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 multiple lines in this array, you should be more careful 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); 
+5
source share

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

 var arr = str[0].split(","); 
+3
source share

let's say we have two dates:

date1: 17/01/1989

date2: 20/02/2000

if we want to compare them, just split the line and compare it as

 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 is greater than date1")}

} else if (a> b) {

alert ("date1 is greater than date2")

} still {

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

}

-one
source share

All Articles