1

I'm losing my mind. I have an array of objects called allAvailabilities

console.log(allAvailabilities[0]);

results in a object with various properties, one of which is:

...
isTrainee: true
...

but

console.log(allAvailabilities[0]['isTrainee']);
console.log(allAvailabilities[0].isTrainee);

results in

false
false

I'm not understanding what is preventing me from accessing the actual isTrainee value of the object property:

console.log(typeof(allAvailabilities[0]['isTrainee']));
console.log(typeof(allAvailabilities[0].isTrainee));
console.log(allAvailabilities[0]['isTrainee'].toString());
console.log(allAvailabilities[0].isTrainee.toString());

results in:

boolean
boolean
false
false

How is this possible? Am I overlooking a fundamental piece of Javascript syntax? Does this have to do with the Boolean class as described here: Why does !new Boolean(false) equals false in JavaScript?

enter image description here

The snippet works fine, what does this mean?

var allAvailabilities = [{_id: '605a5dd76f007e84f28e35fd', begin: '2021-04-03T18:00:00.000Z', end: '2021-04-03T20:30:00.000Z', season: 'regular', hostModel: 'users', isTrainee: true, season: "regular"}];


            console.log(allAvailabilities[0]);
            console.log(typeof(allAvailabilities[0]['isTrainee']));
            console.log(typeof(allAvailabilities[0].isTrainee));
            console.log(allAvailabilities[0]['isTrainee'].toString());
            console.log(allAvailabilities[0].isTrainee.toString());

How can this be? enter image description here

The original allAvailabilities array comes from an API, and each object in the array is modified to have the isTrainee property like so, immediately before calling the code I am having problems with:

enter image description here

Asa LeHolland
  • 372
  • 4
  • 12
  • Please make this reproducible. Include a runnable snippet (using toolbar in editor) that illustrates this behaviour, so we can see it. It is likely you were looking at a different object (in the same array) when you saw that the property was true instead of false. – trincot Jan 12 '22 at 08:23
  • 1
    Another possibility is that your array is being mutated by the rest of your code, but after your `console.log`. In that case the console will show the later content of the objects, not at the time of the `console.log`. See [is console.log lazy?](https://stackoverflow.com/questions/4057440/is-chrome-s-javascript-console-lazy-about-evaluating-objects) – trincot Jan 12 '22 at 08:24
  • @trincot I have added a snippet using the exact same code, and the snippet works as expected. Checking the lazy console.log post now. – Asa LeHolland Jan 12 '22 at 08:33
  • 1
    I confirm it is the lazy stuff, because in your screenshot (which should not be an image! please post as text), it is clear the subscribe callback code is executed *after* you do the `console.log`. – trincot Jan 12 '22 at 08:58
  • Move all your code that depends on the processing you do in the `subscribe` callback, inside that callback (or call a function there, or chain a listener,...etc) – trincot Jan 12 '22 at 09:00

1 Answers1

1

I think it is because there is a promise asynchronous modification of isTrainee in the map. I think changing the map to for may solve the problem. For is a synchronous function rather than asynchronous.