我有兩個數組,需要從一個數組中找到索引並獲取另一個數組索引上的元素。 出於某種原因,一切看起來不錯,但方法仍然返回undefined,但我期待Station_2作爲索引是2通過。它返回undefinedJavascript的foreach問題===不工作
function FindBestStation(x,choosenItem,y)
{
x.forEach(
function(tempItem)
{
if(tempItem===choosenItem)
{
return y[tempItem-1];
}
});
}
var x=[1,2,3,4, 10];
var y=['Station_1','Station_2','Station_3','Station_4','Station_10'];
var choosenItem=2;//another algorithm finds this value
var choosenElement=FindBestStation(x,choosenItem,y);
console.log(choosenElement);
tempItem用作forEach循環指數,這可能無法使用總是在數組變量'x' –
我看到一個問題的提出但沒有真正的問題;你究竟問什麼? – Matt