我从myarray.indexOf(element)
得到-1,即使元素似乎在myarray中。
这是一些代码片段:
function createChangeRecord( old_array, new_array ) {
var nds = new_array.slice(0,new_array.length);
var el, idx;
if (...) {
...
} else if ( old_array.length==new_array.length ) {
for ( var i=0; i<old_array.length; i++ ) {
el = old_array[i];
idx = nds.indexOf(el);
if ( idx!=(-1) ) {
...
} else {
var a = "el: " + el + "; nds: " + nds + "; nds.indexOf(el): " + nds.indexOf(el);
alert( a );
...
}
}
...
}
...
}
警报显示nds确实包含el,但警报只应在idx == - 1时触发,这应该仅在nds不包含el时才为真。
我知道我没有提供足够的信息来确定我的具体问题,但也许有人可以告诉我一些可能导致这种行为的一般原因?
回答一个类似的问题建议使用jQuery inArray()而不是indexOf,但我想知道为什么indexOf不起作用。其他人建议indexOf用于字符串,而不是数组,但是我可以找到的在线文档中并非如此。
使用
nds.indexOf(parseInt(el,10))
其中nds
是一个数组,el
是一个数字(或应该是一个数字)
编辑:
来自msdn:
JavaScript是一种松散类型的语言,这意味着您不会显式声明变量的数据类型。在许多情况下,JavaScript会在需要时自动执行转换。例如,如果向包含文本(字符串)的项目添加数字,则该数字将转换为文本。
我想这种转换是indexOf
返回-1
的原因,因为你的一个数组包含数字和其他包含的字符串。
例如:
old_array = ["10", "20", "30"];
new_array = [10, 20, 30];
以下是我尝试回答你的问题:
它确实有效,我想它也适用于你的情况。当-1
字符串,例如el
,在一系列数字中找不到时,它返回"100"
,例如nds=[100,200]
这是真的。因为"100"
字符串与100
数字不同。
是的,indexOf()
使用数组(数字,字符串或任何对象)以及字符串。但你必须确保检查相同的类型。
为了避免数字与字符串的意外比较,我们可以使用parseInt()
,例如parseInt("123", 10)
返回数字123
。
第二个参数10
称为基数。一个数字(从2到36)代表要使用的数字系统。
> "javascript is awesome".indexOf('v')
2
> [10, 20, 30].indexOf("20")
-1
> [10, 20, 30].indexOf(20)
1
> [10, 20, 30].indexOf( parseInt("20", 10) )
1
> typeof (100)
number
> typeof ("100")
string
> typeof( parseInt( "100", 10))
number
> parseInt( "100", 10)
100
> parseInt("100", 2)
4
> parseInt(11.3, 10)
11
> parseInt(11.3, 2)
3
> [10.3, 11.3, 12.3, 11].indexOf( parseInt(11.3, 10) )
3
检查下面的代码片段,但在运行时请注意alert();
和console.log();
。
function createChangeRecord( old_array, new_array ) {
var nds = new_array.slice( 0, new_array.length ); // this seems to be redundant
var el, idx, msg;
if ( old_array.length == new_array.length ) {
for ( var i=0; i<old_array.length; i++ ) {
el = old_array[i];
idx = nds.indexOf(el);
if ( idx != -1 ) {
msg = "Found: el: " + el + "; nds: " + nds + "; nds.indexOf(el): " + idx + "\n typeof el: " + (typeof el) + "; typepf nds[" + i + "]: " + (typeof nds[i]);
} else {
msg = "Not Found: el: " + el + "; nds: " + nds + "; nds.indexOf(el): " + idx + "\n typeof el: " + (typeof el) + "; typepf nds[" + i + "]: " + (typeof nds[i]);
}
console.log( msg );
alert( msg );
}
}
else {
var err = 'Array lengths are not same';
console.log( err );
alert( err );
}
}
// this will work
var old_array_g = [ 10, 20 ];
var new_array_g = [ 10, 20 ];
createChangeRecord( old_array_g, new_array_g );
// this will not work
var old_array_g = [ "10", "20" ];
var new_array_g = [ 10, 20 ];
createChangeRecord( old_array_g, new_array_g );
// Yes: indesOf works with strings too
var withStrings = "'javascript is awesome'.indexOf('v'): " + "javascript is awesome".indexOf('v');
console.log( withStrings );
alert( withStrings );
// parseInt() returns a number or say integer
var usingParse = "typeof(123): " + typeof( 123 ) + "; typeof( parseInt('123', 10) ): " + typeof ( parseInt('123', 10) ) + "; typeof ('123'): " + typeof('123');
console.log( usingParse );
alert( usingParse );
// parseInt() with base 2
var parseBase2 = "parseInt( '100', 2 ): " + parseInt('100', 2) + "; parseInt( '100' , 10): " + parseInt('100', 10);
console.log( parseBase2 );
alert( parseBase2 );
indexOf
确实有效,并按照你的说法去做。
例如(从控制台演示):
> a = [1,2,3,4,5,6,7,8];
[1, 2, 3, 4, 5, 6, 7, 8]
> b = a.slice(0,a.length);
[1, 2, 3, 4, 5, 6, 7, 8]
> b.indexOf(a[4])
4
如果您收到此错误,则可能意味着您混合了源和目标(点之前的数组是被搜索的数组),或者您有另一个细微的编程错误(就像您没有比较您认为的数组)你在比较)。
在Array上使用indexOf(value)时,它会返回数组中值的索引。
> var testArray = ["a","b","c"];
> testArray.indexOf(1)
-1
> testArray.indexOf("b")
1
> testArray.indexOf("c")
2
> testArray = [10,12,3];
> testArray.indexOf(12)
1
你应该检查你从el获得的类型(el)
以上面的例子为例:
你有idx=nds.indexOf(el)
的地方
用idx=nds.indexOf(''+el+'')
替换它
它在我正在处理的事情中为我解决了类似的问题,但我偶然发现它正在寻找解决方案。
它是否在所有情况下都稳定是我无法回答的。