Skip to main content
added back closing paragraph, slightly improved; also reduced unnecessary deep indent
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

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|eee and bbb|ddd|fff.

        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);

The code works fine, but I am wondering if it's possible to do this in fewer operations / or with faster runtime.

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|eee and bbb|ddd|fff.

        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);

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|eee and bbb|ddd|fff.

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);

The code works fine, but I am wondering if it's possible to do this in fewer operations / or with faster runtime.

Removed closing paragraph (I feel asking for a specific issue about your code makes it off-topic)
Source Link

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|eeeand aaa|ccc|eee and bbb|ddd|fff.

        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);

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

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|eeeand bbb|ddd|fff

        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);

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

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|eee and bbb|ddd|fff.

        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);
Source Link
najib
  • 21
  • 1

reformatting string to two string values in javascript

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|eeeand bbb|ddd|fff

        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);

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