Remove Array Separators

Is it possible to remove separators when you are array.toString? (Javascript)

var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ]; var result = myArray .toString(); 

result shoud be "zeroonetwothreefourfive"

+4
source share
5 answers

You can use join instead of toString -

 var myArray = [ 'zero', 'one', 'two', 'three', 'four', 'five' ]; var result = myArray.join(''); 

join joins all elements of the array into a string using the argument passed to the join function as a separator. Therefore, calling join and passing an empty string should do exactly what you need.

Demo - http://jsfiddle.net/jAEVY/

+15
source

You can simply use Array.join() to achieve this:

 var arr = ["foo", "bar", "baz"], str1 = arr.join(""), // "foobarbaz" str2 = arr.join(":"); // "foo:bar:baz" 

Everything that you specify in the join method will be used as a delimiter.

+6
source

Instead, you can use array.join("separator") .

 ["a", "b", "c"].join(""); // "abc" ["a", "b", "c"].join(" "); // "abc" ["a", "b", "c"].join(","); // "a,b,c" 
+3
source

You can use the replace [MDN] method:

 var arr = [1,2,3,4], arrStr = arr.toString().replace(/,/g, ''); 
+2
source

Usually the separator uses the .replace(/,/g,'') function .replace(/,/g,'') to remove the separator

But if there is a comma in the data, you can consider something like

 var str = ''; for(var x=0; x<array.length; x++){ str += array[x]; } 
+1
source

Source: https://habr.com/ru/post/1384906/


All Articles