14
A
回答
23
因为如果指定的属性/索引在对象中可用,则“in”返回true。 [1,2]是一个数组,并且在0索引处有一个对象。因此,[1,2]中的0和[1,2]中的1。但!(2 [1,2])。
编辑:对于你可能想要的,David Dorward的下面的评论是非常有用的。如果你(有点反常)要坚持“在”,你可以使用对象文本
x = {1: true, 2: true};
这应该允许1 in x && 2 in x && !(0 in x)
等,但说真的,只是使用的indexOf。
2
您可能正在寻找[1,2].indexOf(0)
。 indexOf
可能无法在ie6中运行。
这里是一个实现中,修复它:
if(!Array.indexOf) {
Array.prototype.indexOf = function(obj) {
for(var i=0; i<this.length; i++) {
if (this[i]==obj) {
return i;
}
}
return -1;
}
}
5
,因为在阵列中的第0
元件。
> 0 in [8,9]
true
> 1 in [8,9]
true
> 8 in [8,9]
false
相关问题
- 1. 为什么要列表(1,2)== [2]
- 2. 为什么(Infinity | 0)=== 0?
- 3. 在Javascript中,为什么[1,2] == [1,2]或({a:1})==({a:1})为false?
- 4. 为什么用0
- 5. 为什么$#ARGV 0?
- 6. 为什么0 = 0.5?
- 7. 为什么xspeed!= 0?
- 8. 为什么bytes.length = 0?
- 9. 为什么JavaScript中的“0 === -0”为true?
- 10. 为什么Array(100).map((_,i)=> i + 1)不返回[1,2,...,100]?
- 11. 为什么输出为0?
- 12. 为什么array.size为'0'?
- 13. 如果[0] == 0和0 == [[0]]都为真,为什么[0] == [[0]]为false?
- 14. 为什么signbit(-0)返回0?
- 15. 为什么2 + 1&0结果是0?
- 16. [0] .outerHTML - 为什么需要包含'[0]'?
- 17. Long.valueOf(0).equals(Integer.valueOf(0))为什么是false?
- 18. 为什么InverseFunction [0&] @ 0返回33/10?
- 19. 为什么thing [:] [0]等于thing [0] [:]?
- 20. 为什么0%0会导致1?
- 21. 红宝石为什么0 || 1是0
- 22. 为什么15.5 mod 5 = 0?
- 23. 为什么last_rx总是0?
- 24. 为什么&array!=&array [0]?
- 25. 为什么CreateWindowEx返回0
- 26. 为什么realmin> eps(0)?
- 27. 为什么(false == 0)是TRUE?
- 28. 为什么〜0是-1?
- 29. 为什么Api.getSize返回0
- 30. 为什么mysql_insert_id()返回0?
此外此,Felix正可能寻找'indexOf':https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf(要求JS 1.6,但是URI包括您可以在代码中包含1.5或更低版本的浏览器的实现) – Quentin