最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to replace comma ',' in an array to '' using JavaScript - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 13

This 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('/'));
发布评论

评论列表(0)

  1. 暂无评论