How can I replace ',' with a '/' in an array using JavaScript. Here my array has two string values separated by a ma ',' but I want to make it in form of a single string separating the values by '/' ?
For example: array[] = { value1, a1/a2/a3}
to be replaced by array[] = {value1/a1/a2/a3}
For example: Here I want to replace the oute which es as [3,101/102/103]
by [3/101/102/103].
Can anyone please suggest?
How can I replace ',' with a '/' in an array using JavaScript. Here my array has two string values separated by a ma ',' but I want to make it in form of a single string separating the values by '/' ?
For example: array[] = { value1, a1/a2/a3}
to be replaced by array[] = {value1/a1/a2/a3}
For example: Here I want to replace the oute which es as [3,101/102/103]
by [3/101/102/103].
Can anyone please suggest?
Share Improve this question edited Jan 26, 2022 at 14:32 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Nov 21, 2013 at 10:49 Dojo_userDojo_user 2814 gold badges9 silver badges28 bronze badges 5-
1
Try
array.join('/');
– Tushar Gupta - curioustushar Commented Nov 21, 2013 at 10:52 - 2 please explain more the structure of your array. Is it this way [{ value1, a1/a2/a3},...] – Mohamed Ali JAMAOUI Commented Nov 21, 2013 at 10:52
- 1 Well, may be I didn't get your point but it seems like there are two values stored in your array. And it's array internal structure to separate them. If I didn't get you right, please let me know. – SSC Commented Nov 21, 2013 at 10:55
- @Brian, the appropriate spelling for JavaScript is JavaScript, with a capital J and a capital S. This can be seen in the tag info page. – Heretic Monkey Commented Jan 26, 2022 at 14:32
- 1 @HereticMonkey Yes: Noted. Gosh I've had my pedantry pedanted. :-) – Brian Tompsett - 汤莱恩 Commented Jan 26, 2022 at 14:41
3 Answers
Reset to default 13This will make a one-element array with your plete string:
var newArr = arr.join(',').replace(/,/g, '/').split();
String.prototype.replaceAll = function(target, replacement) {
return this.split(target).join(replacement);
};
array = new Array("a1/a2/a3")
for (i = 0; i < array.length; ++i) {
array[i] = array[i].replaceAll("/",",")
}
does this help?
Another option is
var array = ['1', '2/3/4/5'], array2 = [];
array2.push(array.join('/'));