如何在Stackblitz控制台中显示JavaScript Set()的值?

huangapple go评论50阅读模式
英文:

How to show values of a Javascript Set() in Stackblitz console?

问题

I've been using stackblitz as a scratchpad for learning and testing javascript. I find it very convenient to use the real-time console updates while logging out lines of code.

One problem I've encountered is how the console outputs the results of a Set() method.

For example:

let numbers = [1, 1, 2, 2, 3, 3];
numbers = new Set(numbers);
console.log(numbers); // expect: Set(3) {1, 2, 3}

The output of console.log(numbers) in the Chrome inspector is Set(3) {1, 2, 3}. However, the output of the StackBlitz console is just Set {}.

I suppose this is just a quirk of the SB console, or is there a workaround that doesn't involve opening the Chrome inspector to view the contents of the Set()?

英文:

I've been using stackblitz as a scratchpad for learning and testing javascript. I find it very convenient to use the real time console updates while logging out lines of code.

One problem I've encountered is how the console outputs the results of a Set() method.

For example

let numbers = [1, 1, 2, 2, 3, 3];
numbers = new Set(numbers);
console.log(numbers); // expect: Set(3) {1, 2, 3}

The output of the console.log(numbers) in the chrome inspector is Set(3) {1, 2, 3}
However, the output of the stackblitz console is just Set {}

I suppose this is just a quirk of the SB console or is there a workaround that doesn't involve opening the chrome inspector to view the contents of the Set()?

答案1

得分: 1

最糟糕的情况下,你可以简单地执行:

console.log([ ...numbers ]);

这将把 numbers 展开到一个数组中,然后打印出这个数组。

英文:

Worst case you can simply do:

console.log([ ...numbers ]);

This will spread numbers into an Array, and the Array will be logged.

huangapple
  • 本文由 发表于 2023年6月29日 23:51:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定