Javascript nested arrays with strings -
i have array contains array strings:
var text; var languageset = ["language4", "language5"]; var languages = [ ["language1", "language2", "language3"], languageset, "language6" ]; (function selector() { (i = 0; < languages.length; i++) { (j = 0; j < languages[i].length; j++) { text += "<option value='" + languages[i][j] + "'>" + languages[i][j] + "</options>"; } } document.getelementbyid("languageoptions").innerhtml = text; })();
html:
<select id="languageoptions"> </select>
but everytime run this, output "language 6" ends being such:
l n g u g e 6
how output entire string? don't know why code doesn't work strings , arrays inside arrays.
you should type check see if item array , loop array. used array.isarray here type check. code example working test.
the array.isarray() method returns true if object array, false if not.
var text; var languageset = ["language4", "language5"]; var languages = [ ["language1", "language2", "language3"], languageset, "language6" ]; (function selector() { (i = 0; < languages.length; i++) { if (array.isarray(languages[i])) { (j = 0; j < languages[i].length; j++) { text += "<option value='" + languages[i][j] + "'>" + languages[i][j] + "</options>"; } } else { text += "<option value='" + languages[i] + "'>" + languages[i] + "</options>"; } } document.getelementbyid("languageoptions").innerhtml = text; })();
<select id="languageoptions"> </select>
Comments
Post a Comment