![](/img/trans.png)
[英]How to sort by Array index on Mongoose using a variable instead of string?
[英]Using a variable to search an array instead of a static string
好的,所以我基本上是三個動態下拉框和一個2D數組。 我讓每個框將它們的值加在一起,然后希望通過數組搜索值的總和,以將值所在的行上的第五個值拉出。
var shape = document.getElementById("shape").value;
var dimension_one = document.getElementById("dimension_One").value;
var x = 'x';
var dimension_two = document.getElementById("dimension_Two").value;
var selected_beam = shape + dimension_one + x + dimension_two; // combine all values from text boxes
alert(selected_beam);
for (i = 0; i < array_shapes.length; i++)
{
if (array_shapes[i][2] == selected_beam) {
alert('Area=' + array_shapes[i][5]);
//Area= array_shapes[i][5]);
}
}
我知道選擇的_beam給我想要的值,並且我也知道數組循環從數組中返回我想要的東西,但前提是我要替換
if (array_shapes[i][2] == selected_beam)
與
if (array_shapes[i][2] == "value I want to search for")
因此,我真正需要知道的是-為什么它只接受它作為字符串而不接受我的selected_beam變量。
根據您的數組值,看起來您需要var x為大寫,例如:
var x = 'X';
如果我正確地讀取了陣列,則看起來光束大小在陣列的元素0和1中,而不是在1和2中,因此您可能不需要尋找array_shapes [i] [2],而是尋找array_shapes [i ] [0]或array_shapes [i] [1]
數組中的第一項位於索引值= 0。
您需要進行一些調試。
首先,您需要知道為什么selected_beam
!== “您的值” 。
我建議您使用此函數比較字符串:
function compare( s1, s2 ){
alert("s1: " + s1.toString());
alert("s2: " + s2.toString());
if (s1.toString() == s2.toString())
return alert("true");
return alert("false");
}
>>> compare(selected_beam,"your value");
問題可能很簡單,就像您的selected_beam
中包含不必要的字符一樣。
因此,如果您有alert(selected_beam)
,請嘗試比較字符串,看看它是否返回true或false。
您正在串聯從文本框中解析的值。 結果將是一個字符串
嘗試做:
var selected_beam = parseInt(shape) + parseInt(dimension_one) + parseInt(x) + parseInt(dimension_two);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.