字符串与键的真实性
今天我尝试将 if 语句缩短为:
if (fruit == "apple" || type == "pear" || type == "orange"){
为:
if ([“苹果”、“梨”、“橙子”] 中的水果){
不起作用。 Jonathan Snook 有一个不同的解决方案这里,他基本上使用这样的地图,它确实有效:
< code>if(fruit in {"apple":"", "pear":"", "orange":""}){
为什么它可以工作,而我的简单数组却不能(通常在 Javascript 中)一个物体的存在使该对象返回 true 吗?字符串与键是不同类型的对象吗?我认为数组中存在的字符串也会返回 true。
y = "Stackoverflow";
y && console.log('y is true') // y returns true, so console logs the message
我将原来的解决方案解释为“如果变量fruit的值在这个数组中”。显然事实并非如此。它是否会说“如果变量水果本身在这个数组中?”不,因为这也不起作用:
if (fruit in [fruit, "apple", "pear", "orange"]){
那么 Snook 的 key=>value 版本是什么地图问这是正确的吗?我最好的猜测是,“如果这个映射中变量fruit的值的名称下的键返回true?”
Today I tried shortening an if statement like this:
if (fruit == "apple" || type == "pear" || type == "orange"){
to this:
if (fruit in ["apple", "pear", "orange"]){
It doesn't work. Jonathan Snook has a different solution here where he essentially uses a map like this, which does work:
if(fruit in {"apple":"", "pear":"", "orange":""}){
Why does that work and my simple array doesn't, when usually in Javascript an object's presence makes that object return true? Is a string a different type of object than a key? I thought the string's presence in my array would return true as well.
y = "Stackoverflow";
y && console.log('y is true') // y returns true, so console logs the message
I interpreted my original solution as "if the value of variable fruit is in this array." That's clearly not the case. Does it instead say "if the variable fruit itself is in this array?" No, because this doesn't work either:
if (fruit in [fruit, "apple", "pear", "orange"]){
So what is Snook's version with the key=>value map asking that's correct? My best guess is, "if a key under the name of the value of variable fruit in this map returns true?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果对象
y
具有名为x
的属性,x in y
返回true
(所以是的,你的猜测是正确的)。数组也是对象。数组的属性是索引,它是数字†。这是可行的:
因为数组在索引
0
处有一个元素。这个数组与这个对象相似(但不一样!):(当然数组还有更多的属性,比如
length
、push
、切片
等)在您的对象示例中 (
{"apple":"", "pear":"", "orange":""}),<代码>苹果,
pear
等是对象的属性,而不是属性的值。如何检查数组是否包含元素中描述了如何查明数组中是否包含元素JavaScript 中的对象? 。
†:严格来说,每个属性都是一个字符串,因此即使您使用数字(就像数组一样),它们也会转换为字符串。
x in y
returnstrue
if the objecty
has a property with namex
(so yes, your guess is correct).Arrays are objects too. The properties of an array are the indexes, which are numerical †. This works:
because the array has an element at index
0
. This array is similar (but not the same!) to this object:(of course an array has further properties like
length
,push
,slice
, etc)In your object example (
{"apple":"", "pear":"", "orange":""})
,apple
,pear
etc. are the properties of the object, not the values of properties.How to find out whether an element is contained in an array is described in How do I check if an array includes an object in JavaScript? .
†: Strictly speaking, every property is a string, so even if you use numbers (as it is the case with arrays), they are converted to strings.
要检查某个值是否在数组内,请使用
indexOf
:注意: IE
indexOf
: 9 不支持数组上的indexOf
,但您可以 轻松添加支持。To check if a value is within an array, use
indexOf
:Note: IE < 9 does not support
indexOf
on arrays, but you can add support easily.