首页 > 解决方案 > 如何检查数组列表中的值

问题描述

您好,我有一个无法解决的问题。

我有一个带有数字的表,我想检查第一个数组“选择”中的数字和数组“最终”中的数字。最终数组是 7 个随机生成的数字。我尝试 for 循环以获取唯一匹配但不起作用的数字。

例子

如果我按表中的数字 1,它应该在第一个 for 循环中生成 7 个数字,然后检查数组是否包含选定的任何数字,以及是否删除未选择的数字。

function func1(){
var random = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25];
var selection = [1,2,3,4,5,6,7,8,9,10,11,12];
var numbers = [];
var extracted = [];
    for(i = 0;i < 7; i++){
    var randomPositionl = Math.floor(Math.random() * random.length);
    var final = random.splice(randomPositionl,1);
    numbers.push(final);
    console.log(final);
    }
  
    for (var i = 0; i < selection.length; i++) {
    if (numbers.indexOf(selection[i]) > -1) {
    extracted.push(selection[i]);
    console.log(extracted);
    }
    }
    console.log(extracted);
}

function func2(){
  console.log(2);
}


function func3(){
  console.log(3);
}

function func4(){
  console.log(4);
}

function func5(){
  console.log(5);
}

function func6(){
  console.log(6);
}

function func7(){
  console.log(7);
}

function func8(){
  console.log(8);
}

function func9(){
  console.log(9);
}

function func10(){
  console.log(10);
}

function func11(){
  console.log(11);
}

function func12(){
  console.log(12);
}
table {  
font-family: arial, sans-serif;
  font-size: 16px;
  border-collapse: collapse;
  width: 100%;
  margin-left: auto;
  margin-right: auto;
  table-layout: fixed;
  text-align: center;
  cursor: pointer;
  color: white;
  float:left;
  text-shadow: 2px 2px 4px #000000;
  background-color: black;
}

td {
  border: 2px #a49e9b solid;
}

td:hover {
  background-color: yellow;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<table cellpadding="15" cellspacing="15">
  <tr>
      <td onclick="func1()">1</td>
      <td onclick="func3()">3</td>
      <td onclick="func5()">5</td>
      <td onclick="func7()">7</td>
      <td onclick="func9()">9</td>
      <td onclick="func11()">11</td>
  </tr>
  <tr>
      <td onclick="func2()">2</td>
      <td onclick="func4()">4</td>
      <td onclick="func6()">6</td>
      <td onclick="func8()">8</td>
      <td onclick="func10()">10</td>
      <td onclick="func12()">12</td>
  </tr>
</table>
</body>
</html>

标签: javascriptarrays

解决方案


推荐阅读