JavaScript为同一件事给出了两个不同的结果.使用数字和变量访问数组



$(document).ready(function() {
  pages = new Array(4);
  pages[0] = "here";
  pages[1] = "is";
  pages[2] = "some";
  pages[3] = "data";
  
  j = 1;
  y = j + 1;
  pages[y] = pages[j].slice(2);
  
  $(".output").text(j + " ::: " + (pages[2] != undefined) + " ::: " + (pages[y] != undefined));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="output">text</div>

j = 1;y = j 1;

console.log(j " :::" (页[2]!= undefined) " :::" (页面[y]!= undefined));

返回1 ::: false ::: true

这个错误让我得到了。我无法弄清楚为什么在这两种不同方式中使用的相同数字具有不同的结果。这引起了我的头痛。

结果证明,上面的'j'实际上是一个字符串。从$()。attr()。

阅读后,我没有转换为整数。

最新更新