简体   繁体   English

为什么数组的相应部分在我的代码中不匹配?

[英]Why don't the corresponding parts of the array match up in my code?

I am working on a project in which I have three arrays and an element in one array corresponds to the element in the other arrays with the same index.我正在从事一个项目,其中我有三个 arrays 并且一个数组中的一个元素对应于另一个 arrays 中具有相同索引的元素。 I find a random element of one array by generating a random number, but when I search for the elements in the arrays with this random number as the index, they don't correspond.我通过生成一个随机数来找到一个数组的随机元素,但是当我在 arrays 中以这个随机数作为索引搜索元素时,它们不对应。 How can I make them correspond?我怎样才能使它们对应?

var qbNames = ['Patrick Mahomes', 'Lamar Jackson', 'Russell Wilson', 'Deshaun Watson', 'Drew Brees', 'Aaron Rodgers', 'Ryan Tannehill', 'Kyler Murray', 'Carson Wentz', 'Matt Ryan', 'Dak Prescott', 'Philip Rivers', 'Tom Brady', 'Ben Roethlisberger', 'Josh Allen', 'Matthew Stafford', 'Kirk Cousins', 'Cam Newton', 'Jared Goff', 'Sam Darnold', 'Derek Carr', 'Jimmy Garoppolo', 'Ryan Fitzpatrick', 'Daniel Jones', 'Drew Lock', 'Gardner MINSHEW', 'Dwayne Haskins', 'Baker Mayfield', 'Jameis Winston', 'Jarrett Stidham', 'Justin Herbert', 'Mitch Trubisky', 'Nick Foles', 'Andy Dalton', 'Mason Rudolph', 'David Blough', 'Devlin Hodges', 'Will Grier'];
  var qbTeams = ['KC', 'BAL', 'SEA', 'HOU', 'NO', 'GB', 'TEN', 'ARI', 'PHI', 'ATL', 'DAL', 'IND', 'TB', 'PIT', 'BUF', 'DET', 'MIN', 'FA', 'LAR', 'NYJ', 'LV', 'SF', 'MIA', 'NYG', 'DEN', 'JAC', 'WAS', 'CLE', 'NO', 'NE', 'LAC', 'CHI', 'CHI', 'DAL', 'PIT', 'DET', 'PIT', 'CAR']
  var qbOveralls = ['99', '97', '98', '95', '95', '92', '92', '90', '88', '86', '86', '86', '86', '86', '85', '84', '84', '83', '82', '82', '82', '82', '81', '80', '80', '80', '80', '79', '79', '79', '78', '77', '76', '76', '74', '73', '73', '72']
  var qb1 = Math.floor(Math.random() * Math.floor(37));
  var qbName1 = qbNames[qb1];
  var qbTeam1 = qbTeams[qb1];
  var qbOverall1 = qbOveralls[qb1];
  document.getElementById("qbDisplay1").innerHTML = qbName1 + '\xa0' + qbTeam1 + '\xa0' + qbOverall1;
  qbNames.splice(qb1, 1);
  var qb2 = Math.floor(Math.random() * Math.floor(36));
  var qbName2 = qbNames[qb2];
  var qbTeam2 = qbTeams[qb2];
  var qbOverall2 = qbOveralls[qb2];
  document.getElementById("qbDisplay2").innerHTML = qbName2 + '\xa0' + qbTeam2 + '\xa0' + qbOverall2;
  qbNames.splice(qb2, 2);
  var qb3 = Math.floor(Math.random() * Math.floor(35));
  var qbName3 = qbNames[qb3];
  var qbTeam3 = qbTeams[qb3];
  var qbOverall3 = qbOveralls[qb3];
  document.getElementById("qbDisplay3").innerHTML = qbName3 + '\xa0' + qbTeam3 + '\xa0' + qbOverall3;

When I execute this code, the Names, Teams, and Overalls elements of the same index don't match up.当我执行此代码时,同一索引的 Names、Teams 和Overalls 元素不匹配。 How can I make it that they do correspond and "qbDisplay" would be for example "Patrick Mahomes KC 99"?我怎样才能使它们确实对应并且“qbDisplay”例如是“Patrick Mahomes KC 99”?

You are only removing the randomly selected index from one of the arrays.您只是从 arrays 之一中删除随机选择的索引。 You should do that also for the other two:您也应该对其他两个这样做:

qbNames.splice(qb1, 1);
qbTeams.splice(qb1, 1); // <--- add this 
qbOveralls.splice(qb1, 1); // <--- add this

Of course, the same is true for the second selection, and the third.当然,第二个选择也是如此,第三个也是如此。

It would be better practice, if you would store the three attributes together in objects, so that you only have one array of objects.如果您将三个属性一起存储在对象中,这样您就只有一个对象数组,这将是更好的做法。 Like this:像这样:

var stats = [
  {name: 'Patrick Mahomes', team: 'KC', overall: 99 },
  {name: 'Lamar Jackson', team: 'BAL', overall: 97 },
  // ...etc
];

Another thing I would do, is make use of a shuffle function, that shuffles the above array randomly.我要做的另一件事是使用随机播放 function 随机播放上述数组。 Once you have that, you can just pick the first three (or how many you need).一旦你有了它,你就可以选择前三个(或者你需要多少个)。

Instead of assigning unique id attributes like qbDisplay1 , qbDisplay2 , ...etc, use the class attribute and use the same value for these elements.不要分配唯一的id属性,如qbDisplay1qbDisplay2 、 ...等,而是使用class属性并对这些元素使用相同的值。

I don't see a benefit in the \x0a (non-breaking space), since your data has normal spaces in the name.我看不到\x0a (不间断空格)的好处,因为您的数据名称中有正常空格。 If you don't want things to wrap around, use CSS styling instead ( white-space: nowrap ).如果您不想让东西环绕,请改用 CSS 样式( white-space: nowrap )。

Here is a little demo:这是一个小演示:

 function shuffle(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; } var stats = [ {name: 'Patrick Mahomes', team: 'KC', overall: 99 }, {name: 'Lamar Jackson', team: 'BAL', overall: 97 }, {name: 'Russell Wilson', team: 'SEA', overall: 98 }, {name: 'Deshaun Watson', team: 'HOU', overall: 95 }, {name: 'Drew Brees', team: 'NO', overall: 95 }, {name: 'Aaron Rodgers', team: 'GB', overall: 92 }, {name: 'Ryan Tannehill', team: 'TEN', overall: 92 }, {name: 'Kyler Murray', team: 'ARI', overall: 90 }, {name: 'Carson Wentz', team: 'PHI', overall: 88 }, {name: 'Matt Ryan', team: 'ATL', overall: 86 }, ]; let shuffled = shuffle([...stats]); // take a copy before shuffling for (let display of document.querySelectorAll(".qbdisplay")) { let {name, team, overall} = shuffled.pop(); display.textContent = name + ' ' + team + ' ' + overall; }
 <div class="qbdisplay"></div> <div class="qbdisplay"></div> <div class="qbdisplay"></div>

Conversion tool转换工具

To convert your current data structure to the one proposed above, you can use the following tool.要将您当前的数据结构转换为上面建议的数据结构,您可以使用以下工具。 Copy/paste the three arrays into the input box, and in the output box you'll get the equivalent formatted into the target structure.将三个 arrays 复制/粘贴到输入框中,在 output 框中,您将获得格式化为目标结构的等效格式。 Don't worry about copy/pasting more than needed (you may leave in/out the var... = part).不必担心复制/粘贴超出需要的内容(您可以在var... =部分中留出/留出)。 It will convert quotes to double quotes, so if you have those in a name, you may need to revert that change afterwards:它将引号转换为双引号,因此如果您在名称中有这些,您可能需要在之后恢复该更改:

 let input = document.querySelector("textarea"); let output = document.querySelector("pre"); input.addEventListener("input", refresh); refresh(); function refresh() { let jsons = input.value.replace(/'/g, '"').match(/\[.*?\]/g); let result = []; if (jsons.length === 3) { try { let [names, teams, overalls] = jsons.map(JSON.parse); for (let i = 0; i < names.length; i++) { result.push({ name: names[i], team: teams[i], overall: overalls[i] }); } } catch(e) { output.textContent = e.message; return; } } output.textContent = JSON.stringify(result, null, 2); }
 textarea { width: 100% }
 <textarea> var qbNames = ['Patrick Mahomes', 'Lamar Jackson', 'Russell Wilson', 'Deshaun Watson', 'Drew Brees', 'Aaron Rodgers', 'Ryan Tannehill', 'Kyler Murray', 'Carson Wentz', 'Matt Ryan', 'Dak Prescott', 'Philip Rivers', 'Tom Brady', 'Ben Roethlisberger', 'Josh Allen', 'Matthew Stafford', 'Kirk Cousins', 'Cam Newton', 'Jared Goff', 'Sam Darnold', 'Derek Carr', 'Jimmy Garoppolo', 'Ryan Fitzpatrick', 'Daniel Jones', 'Drew Lock', 'Gardner MINSHEW', 'Dwayne Haskins', 'Baker Mayfield', 'Jameis Winston', 'Jarrett Stidham', 'Justin Herbert', 'Mitch Trubisky', 'Nick Foles', 'Andy Dalton', 'Mason Rudolph', 'David Blough', 'Devlin Hodges', 'Will Grier']; var qbTeams = ['KC', 'BAL', 'SEA', 'HOU', 'NO', 'GB', 'TEN', 'ARI', 'PHI', 'ATL', 'DAL', 'IND', 'TB', 'PIT', 'BUF', 'DET', 'MIN', 'FA', 'LAR', 'NYJ', 'LV', 'SF', 'MIA', 'NYG', 'DEN', 'JAC', 'WAS', 'CLE', 'NO', 'NE', 'LAC', 'CHI', 'CHI', 'DAL', 'PIT', 'DET', 'PIT', 'CAR'] var qbOveralls = ['99', '97', '98', '95', '95', '92', '92', '90', '88', '86', '86', '86', '86', '86', '85', '84', '84', '83', '82', '82', '82', '82', '81', '80', '80', '80', '80', '79', '79', '79', '78', '77', '76', '76', '74', '73', '73', '72'] </textarea> <pre></pre>

Please note that this script must be run after the content is loaded (as happens in the above snippet).请注意,此脚本必须在内容加载后运行(如上面的代码片段所示)。 For it to work make sure your HTML page has the <script>...</script> block right before the closing </body> .要使其正常工作,请确保您的 HTML 页面在关闭</body>之前有<script>...</script>块。 All the other HTML elements should come before the opening of the <script> block.所有其他 HTML 元素应该在<script>块的打开之前出现。

Your code worked correctly just you have to remove founded item from your three list.您的代码可以正常工作,只是您必须从三个列表中删除已建立的项目。

You can make it more organized by using object literals to group the 3 related pieces of information.您可以通过使用 object 文字对 3 条相关信息进行分组来使其更有条理。

`var qbData = [{ name: 'Patrick Mahomes', team: 'KC', overalls: '99' }, { name: 'Lamar Jackson', team: 'BAL', overalls: '97' }, ...];
 var qb1 = Math.floor(Math.random() * Math.floor(37));
 var qbData1 = qbData[qb1];
 document.getElementById("qbDisplay1").innerHTML = qbName1 + '\xa0' + qbTeam1 + '\xa0' + qbOverall1;
 qbData.splice(qb1, 1);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的注册页面即使我正确输入了我的火基代码也不起作用 - why my sign up page don't work even i enter my fire base code correctly 标杆管理:部分内容并非总和 - Benchmarking: parts don't add up to the whole 当新的信息窗口显示在我的代码中时,为什么其他信息窗口没有隐藏? - Why don't other infowindows hide when a new infowindo shows up in my code? 为什么我不能将“ await”移到代码的其他部分? - Why can't I move “await” to other parts of my code? 为什么我的 RegExp 架构在验证 function 上不匹配? - Why my RegExp schema don't match on the validate function? Javascript计算排列-当我不复制数组时,为什么我的代码返回不需要的解决方案? - Javascript computing permutations - why is my code returning unwanted solution when I don't make a copy of the array? 为什么我的日期选择器和时间选择器不显示? - Why don't my date picker and time picker show up? 为什么我的确认“警报”对我的代码不起作用? - Why my confirm “alert” don't work on my code? 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working 我的循环没有分隔与另一个数组不匹配的数组项,我该怎么做? - My loop isn't separating the array items that don't match on the other array, how can I do that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM