mercredi 29 novembre 2017

Trouble comparing index to string literal

I am iterating over an object. For debugging purposes, I'm trying to log when a certain key is hit. For some reason, the if statement below never evaluates to true.

for (let key in tiles) {
    console.log(key)
    if (tiles[key].autoload) {
        app.loadWidget(key);
        if (key === 'social') {
            console.warn("Loading social widget");  //this never gets logged
        }
    }
}

Here is the full output from the first console.log:

10:10:44.111 app.js:357 nearby
10:10:44.112 app.js:357 social   //<--- WTF
10:10:44.113 app.js:357 clock
10:10:44.114 app.js:357 calendar
10:10:44.117 app.js:357 weather
10:10:44.119 app.js:357 addwidget
10:10:44.120 app.js:357 settings
10:10:44.121 app.js:357 account
10:10:44.122 app.js:357 debug

And I tried coercing it to make sure it wasn't a type issue using if ('' + key === 'social'). I also tried ==. It didn't help.

Here is the object definition:

let tiles = {
    'store': {
        name: 'store',
        autoload: true,
        loadOrder: 1,
        /* other attributes */
        isLoaded: false,
    },
    'social': {
        name: 'social',
        autoload: false,
        loadOrder: 1,
        /* other attributes */
        isLoaded: false,
    },
    /* etc, etc */
 }

I'm sure this is something simple I'm overlooking. Why is key never equal to 'social'?

let tiles = {
  'store': {
    name: 'store',
    autoload: true,
    loadOrder: 1,
    /* other attributes */
    isLoaded: false,
  },
  'social': {
    name: 'social',
    autoload: false,
    loadOrder: 1,
    /* other attributes */
    isLoaded: false,
  },
    /* etc, etc */
 }


for (let key in tiles) {
  console.log(key)
  if (tiles[key].autoload) {
    //app.loadWidget(key);
    if ('' + key === 'social') {
      console.warn("Loading social widget");
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire