I have an array in javascript. This array has strings that contains commas (","). I want all commas to be removed from this array. Can this be done?

share|improve this question
Can't you filter the strings before them being pushed in the array? Otherwise is just a simple for loop. – IonuČ› G. Stan Jun 4 '09 at 21:52
I tried to but the strings are coming from other place, dynamically. But it doesn't matter anyway, I figured out what I was doing wrong. I was leaving a comma after every db result. I was convinced that the array push I was doing was adding a comma after every push. I'm a starter in JS. Thanks anyway. – Manny Calavera Jun 4 '09 at 22:05

3 Answers

up vote 15 down vote accepted

Yes.

for(var i=0; i < arr.length; i++) {
 arr[i] = arr[i].replace(/,/g, '');
}
share|improve this answer
1  
+1 for getting closer than me but you need to do something with the result, replace does not mutate the string. – AnthonyWJones Jun 4 '09 at 21:58
1  
Sorry kekoav, Shoudn't it be: arr[i] = arr[i].replace(/,/g, ''); ?? – tekBlues Jun 4 '09 at 21:59
@tekBlues - yes, updated code – Kekoa Jun 4 '09 at 22:01

Sure -- just iterate through the array and do a standard removal on each iteration.

Or if the nature of your array permits, you could first convert the array to a string, take out the commas, then convert back into an array.

share|improve this answer

Given the required string in variable s :-

var result = s.replace(/,/g, '');
share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

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