return 基于输入的数组索引值中的值

return value from array index value based on input

我正在开发一个我不确定如何构建的报告工具 -

基本上,我使用的是一种表单,它接受用户输入并进行转换。

对象的整体结构看起来像这样...

object {
 userInput: ' ',
 available: ' ',
}

一旦用户输入了一些数据,它应该接受输入并更改为不同的数字。

silo_1Lookup = userInput => {
  silo_1DepthsFilling = [926, 893, 860, 827, 794, 761];
};

转换将在 1 个函数中完成,然后更新对象。

伪代码:

if (object.userInput === "0") {
  object.available = "926";
}

每个值将对应于该数组中的一个索引,因此如果 object.userInput === '2.5' 那么 object.avaible 将是 761 (arrayIndex: 6)

一种方法是使用字典将 userInput 映射到 available 值,例如:

const USER_MAPPING = {
  "0": "926",
  "3": "5412",
  "2": "2321"
  // ... rest values
};

// usage
return USER_MAPPING[object.userInput];

这样的映射是硬编码的,容易出现错误,因此如果 userInput 和值背后有任何逻辑,您应该为它生成一些函数:

return generateAvailable(object.userInput)