未捕获的 RangeError:在 JavaScript 中附加到字符串时字符串长度无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28922057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:36:05  来源:igfitidea点击:

Uncaught RangeError: Invalid string length when appending to a string in JavaScript

javascripthtml

提问by axtscz

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:

所以基本上我一直在用 Javascript 开发我的一个小型个人项目,但我遇到了障碍。我正在尝试使用 Javascript 从二维数组生成 HTML 列表。这是我的代码:

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:

它不断抛出 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.

我不知道为什么。我也试过使用 .concat() 来追加,但没有。

回答by Pointy

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

您的二维数组访问不正确,但主要问题是您i在内循环中重新使用变量:

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

That iwill be the same ias 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.

i将与i您的外循环相同。因此,您得到的错误表明您正在构建一个超出运行时系统容量的大字符串。为该循环声明一个新变量。

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

从数组数组访问值需要两组[ ]

entry[j][0]

回答by hoogw

I got this error:

我收到此错误:

Uncaught RangeError: Invalid string length

未捕获的 RangeError:字符串长度无效

And Fix it by use i for outer for loop, use DIFFERENT j for inner for loop.

并通过将 i 用于外部 for 循环来修复它,将不同的 j 用于内部 for 循环。

I have outer for loop use i, and inner for loop still with i, then the error above, took me many hours to figure out the real problem is conflict of 2 i,

我有外部 for 循环使用 i,内部 for 循环仍然使用 i,然后上面的错误,花了我很多小时才弄清楚真正的问题是 2 i 的冲突,

for (var i = 0; i < features.length; i++) {
   for (var i = 0; i < features.length; i++) {
      i;// conflict i will cause error above
   }
}

You must use j or different letters for inner loop

内循环必须使用 j 或不同的字母

回答by Eugene Gr. Philippov

Another source for RangeError: Invalid string lengthis the following example (try to find bug yourself, a task for the reader):

另一个来源RangeError: Invalid string length是以下示例(尝试自己查找错误,这是读者的任务):

var urls = "http://example.org/1";
var url = "http://example.org/2";
for(var i=0;i<100;i++) {
    urls += urls + url + "\r\n"; //on this line, the exception is thrown
}