Converting a string to two strings I have the following code that converts a string that looks like:


  aaa-bbb|ccc-ddd|eee-fff


to two strings that look like:


  aaa|ccc|eeeandbbb|ddd|fff


The code works fine, but I am wondering if I probably missed some string/array functions that can makes the job in fewer operations / or with faster runtime.

var id = "HEL-CAS|MAD-STO|XXX-YYY"
var splittedId = id.split('|');
var from = "",
  to = "";
$(splittedId).each(function(a, b) {
  from = from + b.split("-")[0] + "|";
  to = to + b.split("-")[1] + "|";
});
from = from.substring(0, from.length - 1);
to = to.substring(0, to.length - 1);
You can use some ES6 functions and have something like:

var s = 'HEL-CAS|MAD-STO|XXX-YYY';
var s1 = [];
var s2 = []

for (var v of s.split('|').map(str =&gt; str.split('-'))) {
  s1.push(v[0])
  s2.push(v[1])
}

s1.join('|')
s2.join('|')