Personal Talk:)
Just adding here a way to do with jquery, it also uses regular expression, but it can help a lot in the task of not creating a new Array 0 /
var teste = ['Escova|Corte Masculino','Gel','Cabelo|Corte Masculino']; // Array com os dados
teste = teste.toString().replace('|',',').split(',');
The above code is using 3 jQuery functions.
-
toString()
- converts array to string and separates array positions by "comma"
Example: 'Brush | Cut, Male, Gel, Hair | Male Cut'
-
.replace('|',',')
- this function as the name already says Replaces one value with the other, that is, the first parameter by the second.
In this case, the "|" by ","
-
.split(',')
- and finally the SPLIT function divides the String from the passed parameter, which in turn is , .
At the end the step by step is:
- Get the array and convert it to string (separating the positions with comma)
- Replace the | (pipes) per comma.
- Convert the string back into Array by dividing it every time you find a comma.
The big problem with this solution is that it only applies replace on the first occurrence it encounters, that is, if the array contains more than one element with | it will only replace the first. (boring, right?)
But relax, which is the regular expression to help.
.replace(/\|/g,',')
Using the regular expression causes replace to replace all occurrences of the pipe character ( | ) within the string.
If you are not familiar with the regular expression, here is some good study material:
I want to learn
Anyway, the end result of the method is:
var teste = ['Escova|Corte Masculino','Gel','Cabelo|Barba']; // Array com os dados
teste = teste.toString().replace(/\|/g,',').split(',');
I hope I have helped:)