简体   繁体   English

如何在javascript中使用数组/对象/JSON轻松访问表格数据?

[英]How to easily access tabular data with arrays/objects/JSON in javascript?

I'm working on an issue where I need to access the value and name of a buitenunit, binnenunit and boiler when certain conditions are met.我正在解决一个问题,当满足某些条件时,我需要访问 buitenunit、binnenunit 和锅炉的值和名称。

I have 4 different tables like this (almost similar) and when a condition is met I need to start searching data in 1 of the 4 tables.我有 4 个这样的不同表(几乎相似),当满足条件时,我需要开始在 4 个表中的 1 个中搜索数据。 Take this table for example.以这张表为例。

alpha|    buitenunit              binnenunit              boiler
-----------------------------------------------------------------
4200 |    ERLQ004CV3(value=100)   EHBH04C3V(value=101)    EHVH04S18C3V=(102)
6200 |    ERLQ006CV3(value=200)   EHBH04C3V(value=101)    EHVH04S18C3V=(102)
8200 |    ERLQ008CV3(value=300)   EHBH04C3V(value=101)    EHVH04S18C3V=(102)
11200|    ERLQ011CV3(value=400)   EHBH04C3V(value=101)    EHVH04S18C3V=(102)
14200|    ERLQ014CV3(value=500)   EHBH04C3V(value=101)    EHVH04S18C3V=(102)

Now I need corresponding buitenunit, binnenunit and boiler (and their values) depending on the alpha value.现在我需要相应的 buitenunit、binnenunit 和锅炉(以及它们的值),具体取决于 alpha 值。

For example, say alpha is 4200 then I need to show: "Your selected setup will be ERLQ004CV3, EHBH04C3V and EHVH04S18C3V and it will cost you (100+101+102) 303 coins.例如,假设 alpha 是 4200,那么我需要显示:“您选择的设置将是 ERLQ004CV3、EHBH04C3V 和 EHVH04S18C3V,它将花费您 (100+101+102) 303 个硬币。

For the moment I'm really confused how to get both value and name in way so I can easily access them.目前我真的很困惑如何以某种方式同时获取值和名称,以便我可以轻松访问它们。

This is what I have at the moment:这就是我目前所拥有的:

var alphas = ['4200', '6200', '8200', '11200', '14200', '17000'];
var buitenunit = ['ERLQ004CV3', 'ERLQ006CV3', 'ERLQ008CV3', 'ERLQ011CV3', 'ERLQ014CV3', 'ERLQ016CV3'];
var binnenunit = ['EHBH04C3V', 'EHBH08C3V', 'EHBH08C3V', 'EHBH16C3V', 'EHBH16C3V', 'EHBH16C3V'];
var boiler = ['EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3'];
var grid = {};

    for(var i = 0; i < alphas.length; i++){
        var alpha = alphas[i];
        if(alpha in grid == false){
            grid[alpha] = {};
        }

        var buitenunit = buitenunitlinksboven[i];
        grid[alpha][buitenunit] = i;
        var binnenunit = binnenunitlinksboven[i];
        grid[alpha][binnenunit] = i;
        var boiler = boilerlinksboven[i];
        grid[alpha][boiler] = i;
    }

But now the only way to access the value is if I already know the name of a unit.但是现在访问该值的唯一方法是如果我已经知道一个单位的名称。 And the name is dependant on the alpha value.名称取决于 alpha 值。

So my question is: How would I store this kind of information in a way that it is easy accessible.所以我的问题是:我将如何以易于访问的方式存储此类信息。 Am I currently on the right track?我目前在正确的轨道上吗? I just thought of JSON, can JSON help me perhaps?我只是想到了 JSON,也许 JSON 可以帮助我?

Amazing thanks to anybody taking the time to actually read this.非常感谢任何花时间实际阅读本文的人。

Greetings你好

What about this?那这个呢?

First - build a map of names to values.首先 - 构建名称到值的映射。 This assumes the name and value are always a pair and you can look up one from the other.这假设名称和值始终是一对,您可以从另一个中查找一个。

// first build a map of names to their value
var valueLookupTable = {
  ERLQ004CV3:100,
  ERLQ006CV3:200,
  ERLQ008CV3:300,
  ERLQ011CV3:400,
  ERLQ014CV3:500,
  EHBH04C3V:101,
  EHVH04S18C3V:102
};

Then your grid builder could look more like this那么你的网格构建器可能看起来更像这样

var alphas = ['4200', '6200', '8200', '11200', '14200', '17000'];
var buitenunit = ['ERLQ004CV3', 'ERLQ006CV3', 'ERLQ008CV3', 'ERLQ011CV3', 'ERLQ014CV3', 'ERLQ016CV3'];
var binnenunit = ['EHBH04C3V', 'EHBH08C3V', 'EHBH08C3V', 'EHBH16C3V', 'EHBH16C3V', 'EHBH16C3V'];
var boiler = ['EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3'];

var grid = {};

var nn = alphas.length;
var ii = 0;
 for(; ii < nn; ++ii) {
  var alpha = alphas[ii]
  var entry = { 
    buitenunit : buitenunit[ii],
    binnenunit : binnenunit[ii],
    boiler : boiler[ii]
  }
  /** compute value for that entry by looking up values in the lookup table */
  var val = 0;
  for(var key in entry) {
    val += (valueLookupTable[entry[key]] || 0)
  }
  entry.value = val;
  grid[alpha] = entry;
}

Now when you do现在当你做

grid[4200]

you get你得到

{ buitenunit: "ERLQ004CV3", 
  binnenunit: "EHBH04C3V", 
  boiler: "EKHWS200B3V3", 
  value: 201}

where value is the sum of all the items.其中 value 是所有项目的总和。

If you needed individual values, you can look them up using the item by doing something like如果您需要单独的值,您可以通过执行以下操作使用该项目查找它们

var entry = grid[4200]
var binnenunitValue = valueLookupTable[entry.binnenunit];

Does this address the issue?这是否解决了问题?

Note: I ran this and got the math to work out for me, but it looks like the valueLookupTable is missing some data.注意:我运行了这个并为我计算了数学,但看起来valueLookupTable缺少一些数据。 I suspect your table in the example is not quite complete as I noticed that all the boiler records were the same.我怀疑示例中的表格不够完整,因为我注意到所有锅炉记录都相同。 But in your grid builder code, there is clearly a longer array of values.但是在您的网格构建器代码中,显然有一个更长的值数组。 No matter.不管。 If the valueLookupTable has all of them, the rest should work out just fine.如果valueLookupTable包含所有这些,其余的应该可以正常工作。 It just means you'll need to flesh out that table with the correct values.这只是意味着您需要使用正确的值充实该表。

Update更新

If the unit arrays have corresponding value arrays, you could use the following to build the valueLookupTable based on the arrays with something like this:如果单位数组具有相应的值数组,则可以使用以下valueLookupTable基于具有以下内容的数组构建valueLookupTable

// if you're using underscore.js or jQuery you could use their `extend` method
var hashMerge = function(dest, src) {
  for (var property in src) {
    if (src.hasOwnProperty(property)) {
       dest[property] = src[property];
    }
  }
  return dest;
};
var buildHash = function(keys,values) {
  var result = {};
  var nn = keys.length;
  var ii = 0;
  for (; ii < nn; ++ii) {
     result[keys[ii]]=values[ii];
  }
  return result;
};

var alphas = ['4200', '6200', '8200', '11200', '14200', '17000'];
var buitenUnits = ['ERLQ004CV3', 'ERLQ006CV3', 'ERLQ008CV3', 'ERLQ011CV3', 'ERLQ014CV3', 'ERLQ016CV3'];
var buitenValues = [ 100, 200, 300, 400, 500, 600 ];
var binnenUnits = ['EHBH04C3V', 'EHBH08C3V', 'EHBH08C3V', 'EHBH16C3V', 'EHBH16C3V', 'EHBH16C3V'];
var binnenValues = [ 101, 201, 301, 401, 501, 601 ];
var boilerUnits = ['EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3', 'EKHWS200B3V3']
var boilerValues = [ 102, 202, 302, 402, 502, 602 ];

var buittens = buildHash(buitenUnits, buitenValues);
var binnens = buildHash(binnenUnits, binnenValues);
var boilers = buildHash(boilerUnits, boilerValues);

var valueLookupTable = hashMerge( buittens, hashMerge(binnens, boilers));

The rest should be the same.其余的应该是一样的。

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

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