<i id='c6HeO'><tr id='c6HeO'><dt id='c6HeO'><q id='c6HeO'><span id='c6HeO'><b id='c6HeO'><form id='c6HeO'><ins id='c6HeO'></ins><ul id='c6HeO'></ul><sub id='c6HeO'></sub></form><legend id='c6HeO'></legend><bdo id='c6HeO'><pre id='c6HeO'><center id='c6HeO'></center></pre></bdo></b><th id='c6HeO'></th></span></q></dt></tr></i><div id='c6HeO'><tfoot id='c6HeO'></tfoot><dl id='c6HeO'><fieldset id='c6HeO'></fieldset></dl></div>

        <bdo id='c6HeO'></bdo><ul id='c6HeO'></ul>
      1. <small id='c6HeO'></small><noframes id='c6HeO'>

        <legend id='c6HeO'><style id='c6HeO'><dir id='c6HeO'><q id='c6HeO'></q></dir></style></legend><tfoot id='c6HeO'></tfoot>
      2. 在不使用循环的情况下查找数字数组中的一项

        时间:2023-06-13
          <tbody id='rVrfm'></tbody>
        • <bdo id='rVrfm'></bdo><ul id='rVrfm'></ul>
          <i id='rVrfm'><tr id='rVrfm'><dt id='rVrfm'><q id='rVrfm'><span id='rVrfm'><b id='rVrfm'><form id='rVrfm'><ins id='rVrfm'></ins><ul id='rVrfm'></ul><sub id='rVrfm'></sub></form><legend id='rVrfm'></legend><bdo id='rVrfm'><pre id='rVrfm'><center id='rVrfm'></center></pre></bdo></b><th id='rVrfm'></th></span></q></dt></tr></i><div id='rVrfm'><tfoot id='rVrfm'></tfoot><dl id='rVrfm'><fieldset id='rVrfm'></fieldset></dl></div>

              <small id='rVrfm'></small><noframes id='rVrfm'>

              1. <tfoot id='rVrfm'></tfoot>
                  <legend id='rVrfm'><style id='rVrfm'><dir id='rVrfm'><q id='rVrfm'></q></dir></style></legend>
                  本文介绍了在不使用循环的情况下查找数字数组中的一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这是一个愚蠢的问题,感觉就像一个问题.但是现在精神障碍很糟糕.:(

                  This is a stupid question, it feels like one. But mental block is bad right now. :(

                  我的问题是我有一个仅由数字组成的数组.我想将该数组用作查找,但我传递给在数组中查找数字的数字一直在查找该数字的 index 中的数组,而不是该数字是否存在 /strong> 在数组中.

                  My problem is I have an array consisting only of numbers. I want to use that array as a lookup, but the number I pass to lookup a number in the array keeps looking to the array in the index of that number, not whether that number exists in the array.

                  例如:

                  var a = [2,4,6,8,10],
                  b = 2;
                  
                  if(a[b]){ /* if the number 2 exists in the array a, then do something * }
                  

                  但这要看位置 2 (6) 中的数组值,而不是值 2 是否在数组中.这是完全有道理的,但是(精神障碍)我无法找到一种方法来测试一个数字是否存在于一个数字数组中......我什至将所有内容都设为字符串,但它确实类型强制并且问题仍然存在.

                  But that looks at the array value in position 2 (6), not whether the value 2 is in the array. And this makes perfect sense, but (mental block) I can't figure out a way to test whether a number exists in an array of numbers... I even made everything strings, but it does type coercion and the problem persists.

                  在这里拉我的头发.请帮忙,谢谢.:D

                  Pulling my hair out here. Please help, thanks. :D

                  推荐答案

                  if (a.indexOf(2) >= 0)
                  

                  请注意,IE

                  9 没有indexOf,所以你需要添加它以防它不存在:

                  Note that IE < 9 doesn't have indexOf, so you'll needto add it in case it doesn't exist:

                  if (!Array.prototype.indexOf)
                  {
                    Array.prototype.indexOf = function(searchElement /*, fromIndex */)
                    {
                      "use strict";
                  
                      if (this === void 0 || this === null)
                        throw new TypeError();
                  
                      var t = Object(this);
                      var len = t.length >>> 0;
                      if (len === 0)
                        return -1;
                  
                      var n = 0;
                      if (arguments.length > 0)
                      {
                        n = Number(arguments[1]);
                        if (n !== n) // shortcut for verifying if it's NaN
                          n = 0;
                        else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
                          n = (n > 0 || -1) * Math.floor(Math.abs(n));
                      }
                  
                      if (n >= len)
                        return -1;
                  
                      var k = n >= 0
                            ? n
                            : Math.max(len - Math.abs(n), 0);
                  
                      for (; k < len; k++)
                      {
                        if (k in t && t[k] === searchElement)
                          return k;
                      }
                      return -1;
                    };
                  }
                  

                  这篇关于在不使用循环的情况下查找数字数组中的一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  <i id='OBDop'><tr id='OBDop'><dt id='OBDop'><q id='OBDop'><span id='OBDop'><b id='OBDop'><form id='OBDop'><ins id='OBDop'></ins><ul id='OBDop'></ul><sub id='OBDop'></sub></form><legend id='OBDop'></legend><bdo id='OBDop'><pre id='OBDop'><center id='OBDop'></center></pre></bdo></b><th id='OBDop'></th></span></q></dt></tr></i><div id='OBDop'><tfoot id='OBDop'></tfoot><dl id='OBDop'><fieldset id='OBDop'></fieldset></dl></div>
                    <tbody id='OBDop'></tbody>

                  • <legend id='OBDop'><style id='OBDop'><dir id='OBDop'><q id='OBDop'></q></dir></style></legend>

                      1. <tfoot id='OBDop'></tfoot>
                      2. <small id='OBDop'></small><noframes id='OBDop'>

                        • <bdo id='OBDop'></bdo><ul id='OBDop'></ul>