2017-01-21 7 views
0

変数がstationRentalsHoursの要素と等しい場合はいつでも、変数determinHourを配列stationRentalsHoursと比較しようとしています。その要素を別のArray(stationRentalsHoursTemp)に追加したいと思います。 、一致する値だけが表示されます。私は単純な演算子で試しましたが、それは一時配列に何も入れません。私もJQuery $ .inArrayを使ってみましたが、それは私に奇妙な結果をもたらします。元の配列のものと同じです。この特定のタスクの変数を配列と比較する他の方法はありますか?javascript変数が配列の要素だけを変数とする

ありがとうございました。代わりに

if($.inArray(determineHour, stationRentalsHours[i])){

function updateChart() { 
    if(canvas3){canvas3.destroy();} 

    var determineHour = selectNumber.options[selectNumber.selectedIndex].innerHTML; 
    for (var i = 0; i < stationRentalsHours.length; i++) { 
     /*if(determineHour == stationRentalsHours){ 
     stationRentalsHoursTemp.push(stationRentalsHours[i]);*/ 
     if($.inArray(determineHour, stationRentalsHours[i])){ 
     stationRentalsHoursTemp.push(stationRentalsHours[i]); 
    } 
} 

答えて

0

:あなたの元の状態ではなく、その配列内の個々の要素の配列に文字列を比較しました。平等。 、いっそ

var determineHour = selectNumber.options[selectNumber.selectedIndex].innerHTML; 
for (var i = 0; i < stationRentalsHours.length; i++) { 
    if(determineHour == stationRentalsHours[i]){ 
     stationRentalsHoursTemp.push(stationRentalsHours[i]); 
    } 
} 

使用フィルタ:

var determineHour = selectNumber.options[selectNumber.selectedIndex].innerHTML; 
stationRentalsHoursTemp = stationRentalsHours.filter(function(val){return val == determineHour;}); 
を私は二つのことを混同推測します
0

if条件にわずかな修正でトリックを行うだろう

if($.inArray(determineHour, stationRentalsHours) != -1){

0

あなたのコメントアウトコードを試してみてください。代わりに$ .inArrayを使用しての、あなたは、単にループおよびテストするための指標のために使用することができ、この場合、

function updateChart() { 
    if(canvas3){ 
    canvas3.destroy(); 
    } 
    var determineHour = selectNumber.options[selectNumber.selectedIndex].innerHTML; 
    for (var i = 0; i < stationRentalsHours.length; i++){ 
    if(determineHour == stationRentalsHours[i]){ 
     stationRentalsHoursTemp.push(stationRentalsHours[i]); 
    } 
    } 
} 
関連する問題