字符串与键的真实性

发布于 2024-12-01 14:28:54 字数 860 浏览 1 评论 0原文

今天我尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑脸一如从前 2024-12-08 14:28:54

如果对象y具有名为x的属性,x in y返回true (所以是的,你的猜测是正确的)。

数组也是对象。数组的属性是索引,它是数字。这是可行的:

if(0 in ["apple", "pear", "orange"])

因为数组在索引 0 处有一个元素。这个数组与这个对象相似(但不一样!):

{0: "apple", 1: "pear", 2:"orange"}

(当然数组还有更多的属性,比如lengthpush切片等)

在您的对象示例中 ({"apple":"", "pear":"", "orange":""}),<代码>苹果, pear 等是对象的属性,而不是属性的值。

如何检查数组是否包含元素中描述了如何查明数组中是否包含元素JavaScript 中的对象?


†:严格来说,每个属性都是一个字符串,因此即使您使用数字(就像数组一样),它们也会转换为字符串。

x in y returns true if the object y has a property with name x (so yes, your guess is correct).

Arrays are objects too. The properties of an array are the indexes, which are numerical . This works:

if(0 in ["apple", "pear", "orange"])

because the array has an element at index 0. This array is similar (but not the same!) to this object:

{0: "apple", 1: "pear", 2:"orange"}

(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.

你的呼吸 2024-12-08 14:28:54

要检查某个值是否在数组内,请使用 indexOf

if (["apple", "pear", "orange"].indexOf(fruit) != -1){

注意: IE indexOf: 9 不支持数组上的 indexOf,但您可以 轻松添加支持

To check if a value is within an array, use indexOf:

if (["apple", "pear", "orange"].indexOf(fruit) != -1){

Note: IE < 9 does not support indexOf on arrays, but you can add support easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文