gpt4 book ai didi

JavaScript:console.log() 生成数字而不是字符

转载 作者:行者123 更新时间:2023-12-03 04:59:38 25 4
gpt4 key购买 nike

为什么尝试打印字符串中的每个字符时会得到数字(0 到 9)?字符串的长度是 10,所以我得到每个字母的索引吗?我怎样才能获得 Angular 色呢?我想获得 Python 在类似代码中提供的输出(下面的 JS 和 Python 代码及其自己的输出)。我在 Google Chrome 的控制台上尝试过这个。代码和输出如下:

strng = 'This is me';
for (var charac in strng)
{
console.log(charac);
}

输出为

0
1
2
3
4
5
6
7
8
9

Python 也有 for-in 格式,会打印每行的每个字符。这就是我想要的。来自 iPython 终端的代码和输出如下:

strng = 'This is me'

for charac in strng:
print(charac)

Python 输出为:

T
h
i
s

i
s

m
e

最佳答案

来自MDN :

The for...in statement iterates over the enumerable properties of an object, in arbitrary order. For each distinct property, statements can be executed.

尝试使用of声明:

strng = 'This is me';
for (var charac of strng)
{
console.log(charac);
}

关于JavaScript:console.log() 生成数字而不是字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42286853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com