0

Now, this might just be me not knowing some basics of how TypeScript works, as I am pretty inexperienced with it, but all my attempts to find a solution to this issue have so far failed.

In one of my functions I am trying to get an item-by-item disjunction of two boolean arrays. However, when I am initializing an empty array to then fill with new values, something strange happens.

let res = new Array<boolean>();
console.log(res)

Output of the freshly initialized array "res"

As you can see, while array is depicted as empty in its collapsed form, opening it for more info reveals that it actually already has 5 "false" values in it. (note: it is not always 5 falses, this is just an example)

When the function then starts pushing new values into it, while those values are depicted correctly in the array's preview, in reality there are still the same five "false" values inside it, and those are not being changed in any way. Furthermore, logging specific items from this array returns correct values

res.push(a[i] || b[i])
    console.log("disjunction at position " + i.toString())
    console.log(a[i])
    console.log(b[i])
    console.log(res[i])
    console.log(res)

Console output

This function then returns a new boolean array to be pushed into an array of arrays. When I am logging this array of arrays, it depicts not arrays of values the function filled them with, but instead those weird values that have been put there with initialization. This does not seem to affect the first array (and only that one).

Array of arrays

As I said, I might just miss some crucial information about how arrays in TS/JS work. Right now I am a bit lost, so any help would be appreciated!

4
  • Can you share your entire code? Remember that res is a reference to the underlying array, and your developer tools will typically show the "realtime" version of the array, not just how it looked when it was logged. Commented May 19, 2021 at 13:21
  • I think you're just running into the way the developer tools work. Objects (including arrays) aren't evaluated at the time of the console.log statement, they're evaluated at the time you click on them. So if something changed in between that time, you'll see the array as it exists afterwards. Commented May 19, 2021 at 13:22
  • Please post a Minimal, Reproducible Example Commented May 19, 2021 at 13:23
  • @NicholasTower you're right! There is a filter() function applied to this array after the fact, and it seems all the falses are just arrays that were later thrown away by this filter. Thank you! Commented May 19, 2021 at 13:36

1 Answer 1

1

Turns out it was just me not knowing how dev console works. The big array of arrays is being filtered after being filled up, and because console shows all the arrays in real time, filter causes it to retrospectively depict them as full of falses. Temporarily removing filter proved this hypothesis.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.