gpt4 book ai didi

javascript - indexOf 忽略的数组中的元素

转载 作者:行者123 更新时间:2023-11-29 10:51:32 26 4
gpt4 key购买 nike

我有以下功能。一切正常,除了评论中指出的“if”语句。

function markRow(e) {
var row = e.currentTarget.value ;

var id, rid, field, color, ico ;

rid = markedrows.indexOf(row) ;
if (rid != -1) {
markedrows.splice(rid, 1) ;
ico = ICO_FOLDER + ICO_PLUS ;
color = COLOR_UNMARKED ;
} else {
markedrows.push(row) ;
ico = ICO_FOLDER + ICO_MINUS ;
color = COLOR_MARKED ;
}

e.currentTarget.src = ico ;

for (var col = 1 ; col <= current.cols() ; col++) {
alert(col) ;
alert(markedcols.toString()) ;
// This 'if' doesn't seem to work
if (markedcols.indexOf(col) == -1) {
// This part is ALWAYS executed even when the column is in markedcols
// I get no error messages in firefox
id = "r" + row + "c" + col ;
field = document.getElementById(id) ;
field.style.background = color ;
}
}

markedrows.sort(numericalorder) ;
}

alert 用于调试,它们清楚地表明 markedcols 确实包含了它应该包含的列,但是 indexOf 没有似乎没有捕获它。

据我所知,所有现代浏览器,尤其是 firefox 现在都支持数组上的 indexOf,所以我完全迷路了。我一遍又一遍地检查了代码的逻辑,它应该 可以工作。它只是没有。

最佳答案

我认为你的问题在于:

["1"].indexOf(1) === -1; // true

所以你可能需要 .indexOf(col.toString())

尽管 "1"== 1.indexOf 似乎做了类似 === 的比较。 V8 does, at least.

编辑 specs声明确实应该使用 ===:

Let same be the result of applying the Strict Equality Comparison Algorithm to searchElement and elementK.

If same is true, return k.

关于javascript - indexOf 忽略的数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9345263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com