Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
55
rated 0 times [  57] [ 2]  / answers: 1 / hits: 70510  / 9 Years ago, sun, march 8, 2015, 12:00:00

So basically I've been working on a small personal project of mine in Javascript, and I've hit a snag. I'm trying to generate a list in HTML from a 2D array using Javascript. Here is my code:



function generatePageFromArray(page, array){
var entry, i, j;
for (i = 0; i < array.length; ++i) {
entry = array[i];
for (j = 0; j < entry.length; j = j + 2) {
page = page + '<tr>'+
'<td class=favoritesContainer><input type=checkbox class=favorites></input></td>';
page = page + '<td class=english>' + entry[j,0] + '</td>';
page = page + '<td class=spanish>';
if (entry[j,1].indexOf(|) > 0){
var spanishTerms = entry[j,1].split(|);
var strLength = spanishTerms.size;
for (i = 0; i < strLength; i++){
page = page.concat(spanishTerms[i] + '<br>');
}
}
else{
page = page + entry[j,1];
}
page = page + '</td>'
page = page + '</tr>';
}
}
return page;}


It keeps throwing a Uncaught RangeError: Invalid string length on this line:



page = page + '<tr>'+
'<td class=favoritesContainer><input type=checkbox class=favorites></input></td>';


I have no idea why. I've also tried using .concat() to append, but nothing.


More From » html

 Answers
2

Your 2-D array accesses are incorrect, but the main problem is that you're re-using the variable i in an inner loop:


for (i = 0; i < strLength; i++){
page = page.concat(spanishTerms[i] + '<br>');
}

That i will be the same i as in your outer loop. Thus, the error you're getting is telling you that you're building up a massive string that exceeds the capacity of the runtime system. Declare a new variable for that loop.


Accessing a value from an array of arrays requires two sets of [ ]:


entry[j][0]

[#67532] Thursday, March 5, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jordenfabiand

Total Points: 678
Total Questions: 107
Total Answers: 95

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;