首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >对象内的嵌套数组,使用For循环进行搜索,产生未定义的结果

对象内的嵌套数组,使用For循环进行搜索,产生未定义的结果
EN

Stack Overflow用户
提问于 2019-06-21 04:47:34
回答 1查看 49关注 0票数 0

我已经编辑上传完整的代码,以便在任何浏览器中运行。我在这里得到的只是空格,但我正在寻找1-80 (其中有82,160个组合)中每组3个数字的组合,并希望它输出所有这些结果的数组。然而,目前,数组是空的,但是如果我使用包含jQuery的原始代码,它只给出最后一个值,78,79和80,反复82160次。它给我提供了正确的值数量,但它没有正确填充这些值。

代码语言:javascript
复制
<!doctype html>
<html>
<head>
<title>Keno Tracker v2</title>

<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
    background-color: #f0f0f2;
    margin: 0;
    padding: 0;
    font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;

}
div {
    margin: 0em auto;
    padding: 00px;
    background-color: #fff;
    border-radius: 0em;
}
a:link, a:visited {
    color: #38488f;
    text-decoration: none;
}
@media (max-width: 700px) {
    body {
        background-color: #fff;
    }
    div {
        width: auto;
        margin: 0 auto;
        border-radius: 0;
        padding: 0em;
    }
}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
<div class="mypanel" style="" id="Heading">Last Draw Result</div>
<div class="mypanel" style="all: unset" id="draw"></div><br>
<div class="mypanel" style="all: unset" id="numbs"></div><br>
<div class="mypanel" style="all: unset" id="bon"></div><br>
<div class="mypanel" style="" id="MostDue">Most Overdue Pairs</div>
<div class="mypanel" style="all: unset" id="mosdu"></div><br>
<div class="mypanel" style="" id="MostFreq">Most Frequent Pairs</div>
<div class="mypanel" style="all: unset" id="mosfq"></div><br>
<div class="mypanel" style="display:none" id="test"></div><br>
<div class="mypanel" style="display:none" id="test2"></div><br>
  <script type=text/javascript>
  var results = new Object();
    results.Num = [12354, 12353, 12352, 12351, 12350, 12349, 12348, 12347, 12346, 12345]
    results.Picks = [[1, 2, 4, 6, 8, 9, 13, 15, 38, 39, 22, 25, 65, 44, 66, 75, 80, 34, 12, 77], [5, 2, 4, 38, 39, 7, 8, 9, 18, 14, 23, 25, 65, 44, 66, 75, 80, 34, 12, 77], [1, 2, 3, 7, 9, 10, 14, 15, 26, 21, 63, 41, 67, 71, 38, 39, 79, 32, 17, 72], [11, 12, 14, 16, 18, 19, 13, 15, 26, 27, 61, 43, 62, 72, 79, 38, 39, 37, 10, 74], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [2, 3, 5, 7, 9, 10, 14, 16, 23, 26, 67, 45, 66, 76, 1, 38, 39, 35, 13, 78], [10, 22, 19, 1, 8, 9, 37, 4, 11, 5, 20, 14, 3, 35, 38, 39, 41, 74, 57, 61], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77]]
    results.Bonus = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  var Totals = new Array();
  var temp = new Object();
  var drawsdone = 0
  var Totsdone = 0
  var displayd = 0
function getdraws() {
  var results = new Object()
     results.Num = [12354, 12353, 12352, 12351, 12350, 12349, 12348, 12347, 12346, 12345]
     results.Picks = [[1, 2, 4, 6, 8, 9, 13, 15, 38, 39, 22, 25, 65, 44, 66, 75, 80, 34, 12, 77], [5, 2, 4, 38, 39, 7, 8, 9, 18, 14, 23, 25, 65, 44, 66, 75, 80, 34, 12, 77], [1, 2, 3, 7, 9, 10, 14, 15, 26, 21, 63, 41, 67, 71, 38, 39, 79, 32, 17, 72], [11, 12, 14, 16, 18, 19, 13, 15, 26, 27, 61, 43, 62, 72, 79, 38, 39, 37, 10, 74], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [2, 3, 5, 7, 9, 10, 14, 16, 23, 26, 67, 45, 66, 76, 1, 38, 39, 35, 13, 78], [10, 22, 19, 1, 8, 9, 37, 4, 11, 5, 20, 14, 3, 35, 38, 39, 41, 74, 57, 61], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77], [1, 2, 4, 6, 8, 9, 13, 15, 22, 25, 65, 44, 66, 75, 80, 38, 39, 34, 12, 77]]
     results.Bonus = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
window.drawsdone = 1
}
function getTotals() {
  if (Totsdone == 0){
    for (a=1;a<81;a++){
      for (b=1;b<81;b++){
        if (a<b){
          for (c=1;c<81;c++){
            if (b<c){
              temp.n1 = 0
              temp.n2 = 0
              temp.n3 = 0
              temp.fq123 = 0
              temp.sin123 = 0
              temp.fq12 = 0
              temp.sin12 = 0
              temp.fq13 = 0
              temp.sin13 = 0
              temp.fq23 = 0
              temp.sin23 = 0
              temp.fq1 = 0
              temp.sin1 = 0
              temp.fq2 = 0
              temp.sin2 = 0
              temp.fq3 = 0
              temp.sin3 = 0
              for (i in results.Picks){
                if (results.Picks[i].includes(a)){
                  if (results.Picks[i].includes(b)){
                    if (results.Picks[i].includes(c)){
                      if (temp.sin123 == 0){
                        temp.sin123 = results.Num[0]-results.Num[i]
                      }
                      temp.fq123++
                      temp.n1=a
                      temp.n2=b
                      temp.n3=c
                      }
                    }
                  }
              }
              Totals.push(temp)
            }
          }
        }
      }
    }
    window.Totsdone = 1
  }
}
function display(){
  if (displayd == 0){
    console.log(Totals)
    Totals = new Array()
    window.displayd = 1
  }
}
getdraws();
setInterval(function(){
  getTotals()},2000)
setInterval(function(){
    display()},3000)
  </script>
</body>
</html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-21 05:00:04

因为您的for循环有一个硬编码的第二个条件i<20,而您的Picks数组只有10个项目,例如,从第11个项目开始,您将获得undefined...

编辑:根据更新的问题更新答案。

仍然不确定您的代码的整个逻辑,但是关于

我正在寻找从1到80(其中有82,160个组合)中以3为一组的每一个数字组合,我希望它输出所有这些结果的数组。

我认为这可能是一个更优雅的解决方案:

代码语言:javascript
复制
function* generateDraws(pool, size) { 
	if (size < 1) {
		yield [];
	} else {
		for (let i = size; i <= pool; i++) {
			for (let tail of generateDraws(i - 1, size - 1)) {
				tail.push(i);
				yield tail;
			}
		} 
	}
}

let draws = [];

for (let draw of generateDraws(80, 3)) {
	draws.push(draw);
}

console.log(draws.length);
//console.log(draws); // will not work in stackoverflow snippet runner, does in the browser though!

在处理像这样的大迭代时(如你所说,它有82160个组合,例如80!/(3!(80-3)!),事情可能会变得相当慢,所以使用生成器函数似乎是最合适的,因为它将以恒定的内存使用量运行。

还要注意,生成器函数是现代的JS,所以如果你实际上需要在客户端运行这段代码,而你的用户使用的是旧浏览器,你需要先用babel或typescript转换它,或者在服务器端生成绘图并从服务器获取它们。

希望它至少能有一点帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56693660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档