我有一个包含 144 个索引的数组,我用表单中的数据填充它。我想检查某个索引是否包含某个值,但我不知道 JavaScript 中是否存在这样的函数,或者如何实现该函数。
你能帮我做一个函数,它获取一个数组,某个索引和某个值作为参数,如果存在则返回 true 吗?
一个例子:
var board = new Array();
board.push('X');
board.push('O');
function inArrayatindex(array, index, value)
{
/* if(certain code that i need help with...)
{
return true;
}
}
inArrayatindex(board, 2, 'O'); //returns true
inArrayatindex(board, 2, 'o'); // returns false
inArrayatindex(board, 3, 'X'); // returns false
最佳答案
试试这个:
function check(array, index, value) {
if(index < 0 || index >= array.length) {
return false;
}
return array[index] === value;
}
关于Javascript - 检查某个索引是否在数组中具有特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12444903/