即使元素似乎在myarray中,我也从myarray.indexOf(element)中得到-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 );
                ...
            }
        }
        ...
    }
    ...
}

警报向我显示nds确实包含el,但仅在idx ==-1时才应发出警报,仅当nds不包含el时才应为true。

我知道我没有提供足够的信息来确定我所遇到的具体问题,但是也许有人可以告诉我一些可能导致此问题的一般原因?

建议使用jQuery inArray()代替indexOf来回答类似的问题,但我想知道为什么indexOf无法正常工作。其他人则建议indexOf适用于字符串,而不适用于数组,但是从我可以找到的在线文档中得知并非如此。

最佳答案

使用

nds.indexOf(parseInt(el,10))

其中nds是一个数组,而el是一个数字(或应该是数字)

编辑:

msdn:



而且我猜想这种转换是indexOf返回-1的原因,因为您的数组之一包含数字,而另一个包含字符串。

例如:
old_array = ["10", "20", "30"];
new_array = [10, 20, 30];

以下是我尝试回答您的问题:

为什么indexOf()不起作用?

它确实有效,我想它也适用于您的情况。
当在数字数组(例如)中找不到-1字符串(例如el)时,它返回"100"nds=[100,200]是真的。因为"100"字符串与100编号不同。

indexOf()是否可以与字符串,数组等一起使用?

是的,indexOf()适用于数组(包含数字,字符串或任何对象)以及字符串。但是您必须确保检查相同的类型。

parseInt()有什么作用?

为了避免数字与字符串的意外比较,我们可以使用parseInt(),例如parseInt("123", 10)返回数字123

第二个参数10称为 radix 。代表要使用的数字系统的数字(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 );

09-26 22:01
查看更多