Javascript"=="运算符谎言

Jim*_*son 5 javascript

以下让我很困惑.正如评论中所指出的那样,比较似乎是单独工作的,但是当它们组合在一起时却没有

while应该在同一个月的所有日子里运行,然后将i递增1,然后重新开始.

我已经用console.log完成了整个序列,试图找出它,但它没有任何意义.一切似乎彼此相等,但仍未通过while语句中的"=="测试.

  var i=0;
  var currentdate = 0;
  var currentmonth = 0;
  var opensmonth = 0;
  var opens = [
  { "date":"3/30/2006","zip":"30038","latitude":"33.676358","longitude":"-84.15381"},
  { "date":"4/31/2006","zip":"30519","latitude":"34.089419","longitude":"-83.94701"}
  ];
  intid = setInterval("stepthrough()", 250);
  function stepthrough() {
    //figure out first date.
    if (currentdate == 0) { // we've not been run before
      currentdate = opens[0]["date"];
      currentmonth = currentdate.split("/", 1);
      console.log("Current Month: >" + currentmonth +"<");
    }
    console.log("Current month: " + currentmonth + " And opensdate: " + opens[i]["date"].split("/", 1));

    // 
    // TWILIGHT ZONE ENTERED.
    // 
    if (currentmonth == 3 ) { 
      console.log("Current month equals 3."); // PASSES
    }
    if (opens[i]["date"].split("/", 1) == 3) {
      console.log("Opens date equals 3."); // PASSES
    }
    // BOTH THE ABOVE TESTS PASS IN CHROME AND SAFARI WHAT THE F*$K JAVASCRIPT

    while(opens[i]["date"].split("/", 1) == currentmonth) { // WHY DOESNT THIS WORK I HATE COMPUTERS
      console.log("Trying to add a point one."); 
      addpoint(i);
      i++; 
      console.log("Trying to add a point."); 
    }

    //set the date for next iteration
    currentdate = opens[i]["date"];
    currentmonth = currentdate.split("/", 1);
    console.log ("Current date is now: " + currentdate + " and current month is now: " + currentmonth);
    jQuery('div#date').text(currentdate);

    //if (i>=5000) {
    if (!opens[i]["date"]) {
      console.log("Clearing interval");
      clearInterval(intid);
      //jQuery('div#date').text("Limited at 5000 records")
    }
  }
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 7

JavaScript输入是隐含的.这意味着如果它认为你试图将某个东西视为一个数字,那么最好将该对象视为一个数字,即使它是一个布尔值或一个字符串.

在执行标准时==,JavaScript将使用隐式转换来尝试匹配类型.这通常会导致意外的比较结果.

如果要强制进行强比较,则必须使用===运算符.

话虽这么说,如果您正在检查字符串的"数字"表示,例如"123",并且想要使用强比较,则必须使用parseInt(str,10)将其转换为数字;

有关隐式键入操作的一些示例,请参阅JavaScript真值表答案.


nra*_*itz 4

问题是:["1"] == 1在 Javascript 中,由于 @Matt 描述的隐式转换。但["1"] != ["1"]在 Javascript 中,因为您要比较两个数组,从而比较两个对象,并且只有当它们指向同一个对象时,对象比较才为真,而不是当它们指向两个相同的对象时。

当您分配 with 时.split('/', 1),您将得到一个像 那样的数组['3'],而不是字符串"3"(正如我认为您可能假设的那样)。所以:

currentmonth = currentdate.split("/", 1); // currentmonth is ["3"]
currentmonth == 3; // true, as described above
opens[i]["date"].split("/", 1) == 3; // true, because left-hand evals to ["3"]
opens[i]["date"].split("/", 1) == currentmonth; 
// false, because you're comparing two arrays - ["3"] != ["3"]
Run Code Online (Sandbox Code Playgroud)

要使用当前代码解决此问题,您可以只获取字符串,而不是数组,如下所示:

currentmonth = currentdate.split("/")[0]; // currentmonth is "3"
opens[i]["date"].split("/")[0] == currentmonth; // true, both sides are "3"
Run Code Online (Sandbox Code Playgroud)