简体   繁体   English

使用变量搜索数组而不是静态字符串

[英]Using a variable to search an array instead of a static string

Okay, so what I have is basically three dynamic drop down boxes and a 2D array. 好的,所以我基本上是三个动态下拉框和一个2D数组。 I have each box adding their values together, and then I want the sum of the values to be searched for through the array to pull out the fifth value on whatever the row the value was on. 我让每个框将它们的值加在一起,然后希望通过数组搜索值的总和,以将值所在的行上的第五个值拉出。

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]);
    }
}

I know that selected _beam is giving me the value I want, and I also know that the array loop returns what I want out of the array but only if I replace 我知道选择的_beam给我想要的值,并且我也知道数组循环从数组中返回我想要的东西,但前提是我要替换

if (array_shapes[i][2] == selected_beam)

with

if (array_shapes[i][2] == "value I want to search for")

So what I really need to know is - why will it only accept it as a string and not as my selected_beam variable. 因此,我真正需要知道的是-为什么它只接受它作为字符串而不接受我的selected_beam变量。

Based on your array values, it looks like you need var x to be uppercase like: 根据您的数组值,看起来您需要var x为大写,例如:

var x = 'X';

If I am reading your array correctly, it also looks like the beam size is in element 0 and 1 of the array not 1 and 2, so you may need to not look for array_shapes[i][2], but rather array_shapes[i][0] or array_shapes[i][1] 如果我正确地读取了阵列,则看起来光束大小在阵列的元素0和1中,而不是在1和2中,因此您可能不需要寻找array_shapes [i] [2],而是寻找array_shapes [i ] [0]或array_shapes [i] [1]

The first item in the array is at index value = 0. 数组中的第一项位于索引值= 0。

You need to do some debugging. 您需要进行一些调试。

To start off, you need to know why selected_beam !== "your value" . 首先,您需要知道为什么selected_beam !== “您的值”

I suggest you use this function to compare the strings: 我建议您使用此函数比较字符串:

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");

The problem might be as simple as having unnecessary characters in your selected_beam . 问题可能很简单,就像您的selected_beam中包含不必要的字符一样。

So where you have alert(selected_beam) , try to compare the strings and see if it returns true or false. 因此,如果您有alert(selected_beam) ,请尝试比较字符串,看看它是否返回true或false。

You are concatenating values that you're parsing from a text box. 您正在串联从文本框中解析的值。 The result will be a string 结果将是一个字符串

Try doing: 尝试做:

var selected_beam = parseInt(shape) + parseInt(dimension_one) + parseInt(x) + parseInt(dimension_two);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM