Javascript operator "=="

The following bothers me. As noted in the comments, comparisons seem to work on their own, but when combined, they don’t

At the same time, while should work for all days, then increase by one, and then start again.

I combined the whole sequence with console.log to try to figure this out, but that doesn't make any sense. Everything seems to be alike, but still the test == in the while statement fails.

  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")
    }
  }
+5
source share
4 answers

: ["1"] == 1 Javascript, - @Matt . ["1"] != ["1"] Javascript, , , , - true, , .

.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"]

, , , :

currentmonth = currentdate.split("/")[0]; // currentmonth is "3"
opens[i]["date"].split("/")[0] == currentmonth; // true, both sides are "3"
+4

JavaScript . , , - , , , , .

== JavaScript . .

, ===.

, "" , . "123", , , parseInt (str, 10);

. JavaScript.

+7

, nrabinowitz answer, , .

Javascript == , .

: == . Number String, .

Detail

  • String.split .
  • raw types Javascript. - , Object, Null Undefined.
  • == " (x == y)
  • , (3) - , :

    Type (x) String , Type (y) - Object, x == ToPrimitive (y).

    , ["3"] 3 3 — true (. docs ToPrimitive)

  • , , , ( " (x) (y)" — . :

    true, x y . false.

  • , ( String.split), false.

:

console.log("3 == [3]?", 3 == [3]); // true
console.log("3 == ['3']?", 3 == ['3']);  // true
console.log("'3' == [3]?", "3" == [3]); // true
console.log("'3' == ['3']?", '3' == ['3']); // true
console.log("[3] == [3]?", [3] == [3]); // false
console.log("['3'] == ['3']?", ['3'] == ['3']); // false - NOT SAME OBJECT

var a = ['3'];
var b = a; // SAME OBJECT

console.log("a == b?", a == b); // true!

, @nrabinowitz, , [0] , () .

+1

, , ?

, .

while(opens[i]["date"].split("/", 1) + "str" == currentmonth + "str")
0

All Articles