bai*_*wei 16 javascript arrays indexof
myarray.indexOf(element)即使元素出现在myarray中,我也会得到-1 .
这是一些代码片段:
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 );
...
}
}
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
警报显示nds确实包含el,但警报只应在idx == - 1时触发,这应该仅在nds不包含el时才为真.
我知道我没有提供足够的信息来确定我的具体问题,但也许有人可以告诉我一些可能导致这种行为的一般原因?
回答一个类似的问题建议使用jQuery inArray()而不是indexOf,但我想知道为什么indexOf不起作用.其他人建议indexOf用于字符串,而不是数组,但是我可以找到的在线文档中并非如此.
Aje*_*hah 26
使用
nds.indexOf(parseInt(el,10))
Run Code Online (Sandbox Code Playgroud)
nds数组在哪里是el一个数字(或者应该是一个数字)
编辑:
来自msdn:
JavaScript是一种松散类型的语言,这意味着您不会显式声明变量的数据类型.在许多情况下,JavaScript会在需要时自动执行转换.例如,如果向包含文本(字符串)的项目添加数字,则该数字将转换为文本.
我想这种转换是indexOf返回的原因,-1因为你的一个数组包含数字和其他包含的字符串.
例如:
old_array = ["10", "20", "30"];
new_array = [10, 20, 30];
Run Code Online (Sandbox Code Playgroud)
以下是我尝试回答你的问题:
它确实有效,我想它也适用于你的情况.它返回-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
Run Code Online (Sandbox Code Playgroud)
检查下面的代码片段,但要注意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 );Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
如果您收到此错误,则可能意味着您混淆了源和目标(点之前的数组是正在搜索的数组),或者您有另一个微妙的编程错误(就像您没有比较您认为的数组)你正在比较)。