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 OverflowUse 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 - Remove leading comma from a string - Stack Overflow
javascript - remove first comma of string from arry - Stack Overflow
Remove comma from javascript array - Stack Overflow
javascript - Remove text before first comma - Stack Overflow
I 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 OutputTo remove the first character you would use:
var myOriginalString = ",'first string','more','even more'";
var myString = myOriginalString.substring(1);
I'm not sure this will be the result you're looking for though because you will still need to split it to create an array with it. Maybe something like:
var myString = myOriginalString.substring(1);
var myArray = myString.split(',');
Keep in mind, the ' character will be a part of each string in the split here.
In this specific case (there is always a single character at the start you want to remove) you'll want:
str.substring(1)
However, if you want to be able to detect if the comma is there and remove it if it is, then something like:
if (str[0] == ',') {
str = str.substring(1);
}
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 &.
Split string by , delimiter and remove first item of array using Array.slice() and then join array.
var str = "Kenya, Garden, PFO, Inv 2123, DG, Lot 5543, Ra";
var newStr = str.split(", ").slice(1).join(", ");
console.log(newStr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Also you can find index of first , and get all string after it using String.slice().
var str = "Kenya, Garden, PFO, Inv 2123, DG, Lot 5543, Ra";
var newStr = str.slice(str.indexOf(',')+1).trim();
console.log(newStr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
In the simplest way:
let input = "Kenya, Garden, PFO, Inv 2123, DG, Lot 5543, Ra";
let index = input.indexOf(','); // find the index of first ,
let result = index>-1? input.substring(index+1): input;
You can also add trim(), to remove unwanted white spaces.
This will do it:
if (str.match(/,.*,/)) { // Check if there are 2 commas
str = str.replace(',', ''); // Remove the first one
}
When you use the replace method with a string rather than an RE, it just replaces the first match.
String.prototype.replace replaces only the first occurence of the match:
"some text1, some tex2, some text3".replace(',', '')
// => "some text1 some tex2, some text3"
Global replacement occurs only when you specify the regular expression with g flag.
var str = ",.,.";
if (str.match(/,/g).length > 1) // if there's more than one comma
str = str.replace(',', '');
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 ] ]