对象内的嵌套数组,使用For循环搜索,给出未定义的结果

时间:2019-06-20 20:47:34

标签: javascript arrays for-loop nested javascript-objects

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

<!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>

1 个答案:

答案 0 :(得分:0)

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

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

仍然不确定代码的整体逻辑,但仍不确定

  

我正在寻找1-80(由82,160个组合组成)中的3组数字的每个组合,我希望它输出所有这些结果的数组。

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

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或打字稿进行转换,或者在服务器端生成draws并获取它们从服务器在您的应用中。

希望至少有帮助!