JavaScript:使用 typeof 检查字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12152691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: using typeof to check if string
提问by Leahcim
I'm working on a codecademy.com exercise where we use for-in statements to loop through an object and print hello in different languages by checking to see if the values of the properties in the languages object are strings using typeof
我正在做一个 codecademy.com 练习,我们使用 for-in 语句循环遍历一个对象,并通过检查语言对象中的属性值是否是字符串来使用不同的语言打印 hello typeof
my check to see if the value is a string is not working. my loops giving me this result
我检查该值是否为字符串不起作用。我的循环给了我这个结果
english
french
notALanguage
spanish
The code
编码
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the 3 different languages
for(var hello in languages){
var value = hello;
if (typeof value === "string"){
console.log(value);
}
}
These are the instructions for the exercise
这些是练习的说明
Objects aren't so foreign if you really think about it!
Remember you can figure out the type of a variable by using typeof myVariable. Types we are concerned with for now are "object", "string", and "number".
Recall the for-in loop:
for(var x in obj) { executeSomething(); }
This will go through all the properties of obj one by one and assign the property name to x on each run of the loop.
Let's combine our knowledge of these two concepts.
Examine the languages object. Three properties are strings, whereas one is a number.
Use a for-in loop to print out the three ways to say hello. In the loop, you should check to see if the property value is a string so you don't accidentally print a number.
仔细想想,对象并没有那么陌生!
请记住,您可以使用 typeof myVariable 来确定变量的类型。我们现在关心的类型是“对象”、“字符串”和“数字”。
回想一下 for-in 循环:
for(var x in obj) { executeSomething(); }
这将一一遍历 obj 的所有属性,并在每次循环运行时将属性名称分配给 x。
让我们结合我们对这两个概念的了解。
检查语言对象。三个属性是字符串,而一个是数字。
使用 for-in 循环打印出三种打招呼的方式。在循环中,您应该检查属性值是否为字符串,以免意外打印数字。
回答by Some Guy
That's because you're checking the key
of the object. To check the actual value, you should be doing something like object[key]
. Try this:
那是因为您正在检查key
对象的 。要检查实际值,您应该执行类似object[key]
. 尝试这个:
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the 3 different languages
for(var hello in languages){
var value = languages[hello];
if (typeof value === "string"){
console.log(value);
}
}
回答by JNalewak
Here is the answer: (use typeof and then the object name followed by the var in your for statement and test whether it is equal to "string")
答案如下:(在for语句中使用typeof然后使用对象名称后跟var并测试它是否等于“string”)
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the 3 different languages
for (var x in languages){
if (typeof languages[x] === "string"){
console.log(languages[x]);
}
else ;
}
回答by thomasloh
You are checking keys of the object, not the value. It's usually a good practice to check against the constructor of an object to determine its type.
您正在检查对象的键,而不是值。检查对象的构造函数以确定其类型通常是一种很好的做法。
Something like this:
像这样的东西:
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
for(i in languages) {
if(languages[i].constructor === String) {
console.log(languages[i])
};
};
回答by Roger F. Gay
"string" is not the same as String. I've found one way to test typeof for string.
“字符串”与字符串不同。我找到了一种测试字符串类型的方法。
if (typeof something === typeof "test") ...
You can of course use "string" to compare to, but any actual String will do.
您当然可以使用“字符串”进行比较,但任何实际的字符串都可以。
回答by gomathi
The below coding is also useful to perform only string value.By using variable to access the property list of abject after that by using it check the value is a NotANumber by using isNaN.The code given below is useful to you
下面的编码对于仅执行字符串值也很有用。通过使用变量访问 abject 的属性列表,然后使用它检查值是否为 NotANumber 使用 isNaN。下面给出的代码对您有用
var languages = {
english: "Hello!",
french: "Bonjour!",
notALanguage: 4,
spanish: "Hola!"
};
// print hello in the 3 different languages
for(a in languages)
{
if(isNaN(languages[a]))
console.log(languages[a]);
}
回答by Braulio Santos
this is the for in value to work for me
这是为我工作的价值
for(var x in languages){
for(var x 在语言中){
if(typeof languages[x] === "string"){
console.log(languages[x]);
} else }