简体   繁体   English

如何在jQuery中存储引用表?

[英]How can I store a reference table in jQuery?

I am developing a simple calculator using jquery and html. 我正在使用jquery和html开发一个简单的计算器。 To perform all the calculations needed, this calculator needs to reference a table of values formatted as follows: 为了执行所有需要的计算,此计算器需要引用一个格式如下的值的表:

Machine ID1
    6        52   5655
    7.5      55   2520
    10       60   1013

Machine ID2
    6        55    1255
    7.5      89    5655
    10       90    2020

Part of what this calculator does is it takes in a Machine ID, and it also takes in a row spacing (which can either be 6, 7.5, or 10) - ie the first column. 该计算器所做的部分工作是获取计算机ID,也获取行距(可以为6、7.5或10),即第一列。

The second and third columns are values that are derived from the machine and row spacing. 第二和第三列是从机器和行间距得出的值。

My original plan was to create this calculator solely in jquery and HTML, but I am having a very difficult time figuring out how to implement this reference table in a data structure. 我最初的计划是仅使用jquery和HTML创建此计算器,但是我很难确定如何在数据结构中实现此引用表。 These values in the reference table will not change, so I have no problem hard coding them into javascript. 参考表中的这些值不会更改,因此我将它们硬编码为javascript没问题。

I am aware I could use a database with the MachineID and the RowSpacing as the primary keys, but I feel like creating a new database with a single table that just contains the above info is overkill. 我知道我可以使用MachineID和RowSpacing作为主键的数据库,但是我觉得用一个只包含上述信息的表创建新数据库是过分的。 What options do I have for storing the above data in jQuery that will allow me to query and pull the values? 我有什么选择将上述数据存储在jQuery中,可以让我查询和提取值?

I have investigated using jQuery's data functionality, but that seems to be more towards tying data to individual DOM elements, and I don't think that helps me much. 我已经研究过使用jQuery的数据功能,但这似乎更倾向于将数据绑定到单个DOM元素,并且我认为这对我没有太大帮助。 Am I wrong? 我错了吗? How can I make this work without a database? 没有数据库怎么办?

If these are static values, I think you'll be best served by storing them in a JSON object, like this: 如果这些是静态值,我认为将它们存储在JSON对象中将是最好的选择,例如:

var ID1 = {
    6 : {
        "value1": 52,
        "value2": 5655
    },
    7.5 : {
        "value1": 55,
        "value2": 2520
    },
    10 : {
        "value1": 60,
        "value2": 1013
    }
}

var ID2 = {
    6 : {
        "value1": 55,
        "value2": 1255
    },
    7.5 : {
        "value1": 89,
        "value2": 5655
    },
    10 : {
        "value1": 90,
        "value2": 2020
    }
}

This way, if you needed to refer to the machine name ID2 , where row spacing is 10 , and get the second of your two values, you'd do it as follows: 这样,如果您需要引用机器名称ID2 ,其中行间距为10 ,并获取两个值中的第二个,则可以按以下步骤进行操作:

Result = ID2[10]["value2"];

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

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