如何在 Javascript 或 Jquery 中处理具有多个循环的大量数组?

Posted

技术标签:

【中文标题】如何在 Javascript 或 Jquery 中处理具有多个循环的大量数组?【英文标题】:How to handle a large numbers of arrays with multiple loops in Javascript or Jquery? 【发布时间】:2018-10-22 12:50:35 【问题描述】:

我正在尝试为这样的字典生成单词。

html

 <div class="print"></div>

javascript

    var c = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","r","s","t","u","v","w","x","y","z"]
    var v = ["a","e","i","o","u"]

    var gen = '';
    for (i = 0; i = c.length; i++) 
        for (j = 0; j < v.length; j++) 
            gen += c[i] + v[j] + '<br>';
        
    
    $('.print').html(gen);

但我的浏览器无法处理循环。

它会显示这样的消息。

哇,啪!显示此网页时出现问题。


上面的数组只是一个例子,实际的数组是这样的。

 var c = ["ၵ", "ၶ", "င", "ၸ", "သ", "ၺ", "တ", "ထ", "ၼ", "ပ", "ၽ", "ၾ", "မ", "ယ", "ရ", "လ", "ဝ", "ႁ", "ဢ"];

有些数组超过 40 个字符。

虽然我将循环减少了两次,但它不起作用。

1 -

2 - 如果有可以生成大量单词的好软件或程序,请推荐我。

【问题讨论】:

i = c.length; 应该是i &lt; c.length;。错字问题 感谢@CertainPerformance,我在文本编辑器中使用了不同的字体是我的错误。 【参考方案1】:

您的代码中有一个类型。正确的 JavaScript sn-p 是这样的:

var c = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","r","s","t","u","v","w","x","y","z"]
var v = ["a","e","i","o","u"]

var gen = '';
for (i = 0; i < c.length; i++) 
    for (j = 0; j < v.length; j++) 
        gen += c[i] + v[j] + '<br>';
    

$('.print').html(gen);

只需检查父循环应该是这样的:i &lt; c.length;

【讨论】:

错字问题是题外话。回答不合适 - 发表评论并投票关闭(或标记)。

以上是关于如何在 Javascript 或 Jquery 中处理具有多个循环的大量数组?的主要内容,如果未能解决你的问题,请参考以下文章

如何在javascript或jquery中使用变量作为数组键[重复]

如何在 jQuery / javascript 中使用 each() 进行迭代或循环

如何在 Javascript 或 jQuery 中将标签包装在评论中 [重复]

如何在 Javascript 或 Jquery 中处理具有多个循环的大量数组?

如何使用 Javascript 或 Jquery 将 JSON 对象包装在数组中?

如何在指定字符 jQuery 或 JavaScript 之前抓取子字符串