1

I have problem with split. I have a long string, and i want fill it in 3 dimensional array.

var str = "normal/zapnout-0-1/vypnout-1-1%nocni/zapnout-2-1/vypnout-3-1*list/film-4-1/manual-5-1/auto-6-1*obyvak/světlo-7-1/krb-8-1/žaluzie-9-1/televize-10-1%světlo-11-1/okno-12-1%světlo-13-1/okno-14-1%pokoj/světlo-15-1";
 var res = str.split("*");
 var res2 = new Array();
 var res3 = new Array();

 for (var i = 0; i < res.length; i++) {
     res2[i] = res[i].split("%");
     for (var t = 0; t < res2[i].length; t++) {
         res3[i]= new Array();
         res3[i][t]=res2[i][t].split("/");
     }
 }
 document.getElementById("demo2").innerHTML = res3;

But problem is, result from res3 is:

,nocni,zapnout-2-1,vypnout-3-1,list,film-4-1,manual-5-1,auto-6-1,,,,pokoj,světlo-15-1

I don't know what the problem is.

1
  • please use var res2 = []; much more elegant Commented Mar 9, 2014 at 13:50

2 Answers 2

1

Try this:

 var str = "normal/zapnout-0-1/vypnout-1-1%nocni/zapnout-2-1/vypnout-3-1*list/film-4-1/manual-5-1/auto-6-1*obyvak/světlo-7-1/krb-8-1/žaluzie-9-1/televize-10-1%světlo-11-1/okno-12-1%světlo-13-1/okno-14-1%pokoj/světlo-15-1";
 var res = str.split("*");
 for(var i=0; i< res.length;i++){
     res[i] = res[i].split("%");
     for(var j=0;j< res[i].length;j++){
         res[i][j] = res[i][j].split("/");
     }
 }
 console.log(res);

Also just for you to know, your code has the issue of reinitializing res3[i] instead of making it res3[i][t]. This should be your code :

 var str = "normal/zapnout-0-1/vypnout-1-1%nocni/zapnout-2-1/vypnout-3-1*list/film-4-1/manual-5-1/auto-6-1*obyvak/světlo-7-1/krb-8-1/žaluzie-9-1/televize-10-1%světlo-11-1/okno-12-1%světlo-13-1/okno-14-1%pokoj/světlo-15-1";
var res = str.split("*");
var res2 = new Array();
var res3 = new Array();

for (var i = 0; i < res.length; i++) {
    res2[i] = res[i].split("%");
    res3[i] = new Array();
    for (var t = 0; t < res2[i].length; t++) {
        res3[i][t]= new Array();
        res3[i][t]=res2[i][t].split("/");
    }
}

But please stop using the variables in this way because for a big application this will be a memory killer.

Sign up to request clarification or add additional context in comments.

Comments

1

Try the below. By default JavaScript strips the brackets and quotes when printing arrays.

document.getElementById("demo2").innerHTML = JSON.stringify(res3);

Here is a working implementation of the logic:

var res3 = str.split('*').map(function(segment) {
    return segment.split('%').map(function(segment) {
        return segment.split('/');
    });
});

6 Comments

That say [[null,["nocni","zapnout-2-1","vypnout-3-1"]],[["list","film-4-1","manual-5-1","auto-6-1"]],[null,null,null,["pokoj","světlo-15-1"]]]
Then I don't know what you are expecting.
I want [[["normal/zapnout-0-1/vypnout-1-1"],["nocni","zapnout-2-1","vypnout-3-1"]],[["list","film-4-1","manual-5-1","auto-6-1"]],[["obyvak","světlo-7-1","krb-8-1","žaluzie-9-1","televize-10-1"],["světlo-11-1","okno-12-1"],["světlo-13-1","světlo-141"],["pokoj","světlo-15-1"]]] Without null.
@sabof I up voted your answer, but try to give an answer without the use of ES5 because these kind of "developers" will start asking about the errors that they receive because of the use of map function in IE 8. ;)
@helly0d thanks. I just thought that this makes much more sense with .map.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.