Fala Pessoal :)
Just by adding here a way to do with jquery, it also uses regular expression, but 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 the array into a string and separates the array positions by "comma"
Example : 'Brush|Cut, Male,Gel,Hair|Men’s Cut'
.replace('|',',')
- this function as the name already says Replaces one value by the other, ie, the first parameter by the second.
In this case the "|" for ","
.split(',')
- and finally the SPLIT function divides the String from the parameter passed, which in turn is ",".
In the end the step by step is :
- Takes the array and converts to string (separating the positions with comma)
- Replaces | (Pipes) by comma.
- Convert the string to Array again by splitting it every time you find a comma.
The big problem with this solution is that it applies replace only to the first occurrence it encounters, i.e., if the array contains more than one element with | it will only replace the first. (boring right?)
But relax, that here comes the regular expression to help.
.replace(/\|/g,',')
Using the regular expression causes replace to replace all pipe character occurrences (|) inside the string.
If you do not have family with regular expression, follow a good material for study:
I want to learn
Finally, the final 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’ve helped :)
Excellent! Thank you so much for your help!
– André Albson
Good answer. I’ve been thinking about how functionalists do it. I believe a map inserting arrays with arrays for multiple items, followed by a Flatten. I don’t know....
– vinibrsl