Use the join method:
alert(testarray.join("%")); // 'a%b%c'
Here's a working example. Note that by passing the empty string to join you can get the concatenation of all elements of the array:
alert(testarray.join("")); // 'abc'
Side note: it's generally considered better practice to use an array literal instead of the Array constructor when creating an array:
var testarray = ["a", "b", "c"];
Answer from James Allardice on Stack OverflowI created a div element and appended an array into it. However, when the array is printed, it shows the commas as well. Is there a way to remove the commas when printing the array? (Please ignore the really weird choice of words, I just placed random words in the array to test something).
Input Output") You donโt even need the map in this case.
Use the join method:
alert(testarray.join("%")); // 'a%b%c'
Here's a working example. Note that by passing the empty string to join you can get the concatenation of all elements of the array:
alert(testarray.join("")); // 'abc'
Side note: it's generally considered better practice to use an array literal instead of the Array constructor when creating an array:
var testarray = ["a", "b", "c"];
you can iterate through the array and insert your characters
var testarray=new Array("a","b","c");
var str;
for (var i = 0; i < testarray.length; i++) {
str+=testarray[i]+"%";
}
alert(str);
javascript - How do I remove commas from an array of strings - Stack Overflow
node.js - Remove comma from value box in array - Stack Overflow
Remove comma from javascript array - Stack Overflow
How to remove comma and "" from JavaScript Array? - Stack Overflow
You can use JavaScript Array#map with RegEx to remove extra commas.
arr.map(e => e.replace(/(,\s*)+/, ','));
ES5 Equivalent:
arr.map(function (e) {
return e.replace(/(,\s*)+/, ',');
});
RegEx Demo
The regex (,\s*)+ will search for one or more commas separated by any number of spaces between them.
Show code snippet
var arr = ["1 Trenchard Road, , , , Saltford, Bristol, Avon", "10 Trenchard Road, , , , Saltford, Bristol, Avon", "11 Trenchard Road, , , , Saltford, Bristol, Avon", "12 Trenchard Road, , , , Saltford, Bristol, Avon"];
arr = arr.map(e => e.replace(/(,\s*)+/, ', '));
console.log(arr);
document.getElementById('result').innerHTML = JSON.stringify(arr, 0, 4);
<pre id="result"></pre>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can split by coma, filter blanks and join.
var str = "1 Trenchard Road, , , , Saltford, Bristol, Avon";
var result = str.split(',').filter(x => x.trim()).join()
console.log(result); // 1 Trenchard Road, Saltford, Bristol, Avon
Notice: used ES6 arrow function (=>) you can replace it with classical function if it does not work in your environment.
Full example with map function:
let arr = [
'1 Trenchard Road, , , , Saltford, Bristol, Avon',
'10 Trenchard Road, , , , Saltford, Bristol, Avon',
'11 Trenchard Road, , , , Saltford, Bristol, Avon',
'12 Trenchard Road, , , , Saltford, Bristol, Avon'
];
let result = arr.map(i => i.split(',').filter(x => x.trim()).join());
ES5 Equivalent:
var result = arr.map(function(i) {
return i.split(',').filter(function(x) {
return x.trim();
}).join();
});
To remove the commas from a string you could simply do
s = s.replace(/,/g,'');
But in your specific case, what you want is not to add the commas. Change
location.href = '/Sample.aspx?' + arr;
to
location.href = '/Sample.aspx?' + arr.join('');
What happens is that adding an array to a string calls toString on that array and that function adds the commas :
""+["a","b"] gives "a,b"
Don't rely on the implicit string conversion (which concatenates the array elements with a comma as separator), explicitly .join the array elements with &:
var arr = [];
for (var i = 0; i < str_array.length; i++) {
str_array[i] = str_array[i].replace(/^\s*/, "").replace(/\s*$/, "");
arr.push(str_array[i] + '=1');
}
location.href = '/Sample.aspx?' + arr.join('&');
Think about it like this: You have a set of name=value entries which you want to have separated by &.
Hi all, I'm trying to remove commas from my numbers that are in an array :
I know the method '.toFixed(2)' but I'm having trouble applying it in a .map
This is what I did:
And this is what I get :
Unfortunately I added the two values โโso I don't get an array anymore.
What would be the right way to find an array like I had but without a comma?
How do I merge them into my array? ๐
You can use map() for this.
var arr = [ [ [ 10, 0 ] ], [ [ 8, 0 ] ], [ [ 8, 0 ] ], [ [ 5, 2 ] ] ];
var result = arr.map(function(a) {
return a[0];
});
console.log(result)
You could do this by changing where the join happens and pre/app-ending some square brackets, e.g.
var line = arr.map(e => e.map(f => f.join(",")));
console.log('[' + line.join('],[') + ']');
// [10,0],[8,0],[8,0],[5,2]
I do have to ask though, why are you getting back a set of arrays each with a single value? Is it possible to avoid getting a dataset like that in the first place? You could avoid the double map/foreach that way.
For instance if you had one level less nesting in your source array the map line would become a little simpler
var arr = [ [ 10, 0 ], [ 8, 0 ], [ 8, 0 ], [ 5, 2 ] ];
var line = arr.map(f => f.join(","));
console.log('[' + line.join('],[') + ']');
This is of course if you want to specifically output the string for the array matrix, if you just wanted a flatter version of your original array then you could do:
var newList = arr.map(f => f[0]);
// [ [ 10, 0 ], [ 8, 0 ], [ 8, 0 ], [ 5, 2 ] ]