一个数组或许多吗?(哈希表)

时间:2022-05-07 07:41:30

I've an array that is being used to store the conversion factors for a conversion program I'm currently working on.

我有一个数组,用来存储转换程序的转换因子,我现在正在进行这个转换程序。

A short Example:

一个简短的例子:

var Length =
{
"lengthsA" :
{
    "inch" : 0.0254,
    "yard" : 0.9144,
    "mile" : 1609.344,
    "foot" : 0.3048,
    "metres": 1
}}

This will become much bigger and there are many more of them.

这将变得更大,而且还有更多。

It seems I have two options. I can either declare many arrays, one for each conversion type and in the function use and if else to dictate which one should be called upon for the conversion. The alternative is to use one huge array that stores everything. This would nullify the need for an if else and also remove the need to declare many arrays but at the cost of combining everything into what could become one big mess.

看来我有两个选择。我可以声明多个数组,一个用于每个转换类型,另一个用于函数使用,如果其他命令指示应该调用哪个数组来进行转换。另一种选择是使用一个巨大的数组来存储所有东西。这将会使对if的需求化为乌有,同时也消除了声明多个数组的需要,但是代价是将所有的东西合并到一个可能会变成一个大混乱的地方。

I'm in favour of the first option, mainly because I like modularity and it'd be easier for debugging / editing.

我支持第一个选项,主要是因为我喜欢模块化,而且调试/编辑更容易。

I'm also concerned about speed and access time. With one large array would there be an impact seeing as I'm using keys to determine what values are called. Key above would be "lengthsA"

我还关心速度和访问时间。如果使用一个大数组,就会产生影响,因为我正在使用键来确定调用了什么值。上面的键是"lengthsA"

Thanks.

谢谢。

3 个解决方案

#1


2  

If I were doing this project, I'd definitely use a hierarchical structure. I might start with something like this:

如果我在做这个项目,我肯定会使用层次结构。我可以从这样的东西开始:

var conversions = {
    length : {
        lengthsA : {
            inch : 0.0254,
            yard : 0.9144,
            mile : 1609.344,
            foot : 0.3048,
            metres: 1
        },
        lengthsB : {
            . . .
        }
    },
    mass : {
    },
    . . .
}

The structure is: conversions.<category>.<conversion_group>.<unit_name>. It's probably as easy to maintain as any other structure.

结构是:转换。 <目录> 。< conversion_group >。< unit_name >。它可能和其他结构一样易于维护。

You might consider adding a property reference that would indicate the name of the unit that should be the reference (e.g., reference : "metres" in the case of lengthsA). I'd also be more consistent about unit names ("inch" is singular; "metres" is plural). Depending on your application, you might also want to have each conversion be a structure with a value and an uncertainty. (Some conversion factors are exact; others are not.)

您可以考虑添加一个属性引用,该引用将指示应该引用的单元的名称(例如,在lengthsA的情况下引用:“米”)。我对单位名称也更一致(“inch”是单数;“米”是复数)。根据您的应用程序,您可能还希望每个转换都是具有值和不确定性的结构。(有些换算因子是准确的;其他人不是。)

#2


1  

Hard to say without knowing all the details of your program, but I wouldn't use hierarchical objects for storing units, but rather a flat array, similar to a SQL table:

很难在不知道程序的所有细节的情况下说,但是我不会使用层次对象来存储单元,而是使用一个类似于SQL表的扁平数组:

units = [
    { category: "length", name: "inch" , value: 0.0254   },
    { category: "length", name: "yard" , value: 0.9144   },
    { category: "length", name: "mile" , value: 1609.344 },
    { category: "length", name: "foot" , value: 0.3048   },
    { category: "length", name: "meter", value: 1        }
]

You will need a couple of utility functions to find items in this table (like getUnitsByCategory), but once you've got it, you'll find this structure much easier to work with. Uniformity is the king!

您将需要一些实用函数来查找这个表中的项(比如getUnitsByCategory),但是一旦您找到了它,您就会发现这个结构更容易使用。一致性是国王!

#3


0  

if you define variable for javascript so..

如果你为javascript定义变量。

var inch=0.0254,
yard=0.9144

youcan write

你可以写

<option>inch</option>

and acces it with

,acc

window[document.select.textContent]

it's much faster but the code would be much longer.

它的速度要快得多,但代码要长得多。

In your case the readability is more important

在你的情况下,可读性更重要。

so yes create a multidiminsional object.(groups)

所以,是的,创建一个多变量对象。

it's also easier to access the values.

它也更容易访问值。

obj={
 "length":{
  inches:0.0254,
  miles:1609.344,
 },
 "weight":{
  kg:1  
 }
}

so you can access it by

你可以访问它。

obj.length.inches

or

 obj['length']['inches']

and write

和写

window.onload=function(){
var obj={
 length:{
  inches:0.0254,
  miles:1609.344,
 }
}
var select1=document.createElement('select'),
select2=null,
f=document.createDocumentFragment(),
input=document.createElement('input'),
convert=document.createElement('button');
for(var a in obj.length){
 f.appendChild(document.createElement('option')).textContent=a;// easyway to access
}
select1.appendChild(f);
select2=select1.cloneNode(true);
input.type='text';
convert.textContent='Convert';

convert.addEventListener('click',function(e){
 console.log(
  input.value,
  obj.length[select1.textContent],// easyway to access
  obj.length[select2.textContent]// easyway to access
)
},false);

var bdy=document.body
bdy.appendChild(input);
bdy.appendChild(select1);
bdy.appendChild(select2);
bdy.appendChild(convert);
}

#1


2  

If I were doing this project, I'd definitely use a hierarchical structure. I might start with something like this:

如果我在做这个项目,我肯定会使用层次结构。我可以从这样的东西开始:

var conversions = {
    length : {
        lengthsA : {
            inch : 0.0254,
            yard : 0.9144,
            mile : 1609.344,
            foot : 0.3048,
            metres: 1
        },
        lengthsB : {
            . . .
        }
    },
    mass : {
    },
    . . .
}

The structure is: conversions.<category>.<conversion_group>.<unit_name>. It's probably as easy to maintain as any other structure.

结构是:转换。 <目录> 。< conversion_group >。< unit_name >。它可能和其他结构一样易于维护。

You might consider adding a property reference that would indicate the name of the unit that should be the reference (e.g., reference : "metres" in the case of lengthsA). I'd also be more consistent about unit names ("inch" is singular; "metres" is plural). Depending on your application, you might also want to have each conversion be a structure with a value and an uncertainty. (Some conversion factors are exact; others are not.)

您可以考虑添加一个属性引用,该引用将指示应该引用的单元的名称(例如,在lengthsA的情况下引用:“米”)。我对单位名称也更一致(“inch”是单数;“米”是复数)。根据您的应用程序,您可能还希望每个转换都是具有值和不确定性的结构。(有些换算因子是准确的;其他人不是。)

#2


1  

Hard to say without knowing all the details of your program, but I wouldn't use hierarchical objects for storing units, but rather a flat array, similar to a SQL table:

很难在不知道程序的所有细节的情况下说,但是我不会使用层次对象来存储单元,而是使用一个类似于SQL表的扁平数组:

units = [
    { category: "length", name: "inch" , value: 0.0254   },
    { category: "length", name: "yard" , value: 0.9144   },
    { category: "length", name: "mile" , value: 1609.344 },
    { category: "length", name: "foot" , value: 0.3048   },
    { category: "length", name: "meter", value: 1        }
]

You will need a couple of utility functions to find items in this table (like getUnitsByCategory), but once you've got it, you'll find this structure much easier to work with. Uniformity is the king!

您将需要一些实用函数来查找这个表中的项(比如getUnitsByCategory),但是一旦您找到了它,您就会发现这个结构更容易使用。一致性是国王!

#3


0  

if you define variable for javascript so..

如果你为javascript定义变量。

var inch=0.0254,
yard=0.9144

youcan write

你可以写

<option>inch</option>

and acces it with

,acc

window[document.select.textContent]

it's much faster but the code would be much longer.

它的速度要快得多,但代码要长得多。

In your case the readability is more important

在你的情况下,可读性更重要。

so yes create a multidiminsional object.(groups)

所以,是的,创建一个多变量对象。

it's also easier to access the values.

它也更容易访问值。

obj={
 "length":{
  inches:0.0254,
  miles:1609.344,
 },
 "weight":{
  kg:1  
 }
}

so you can access it by

你可以访问它。

obj.length.inches

or

 obj['length']['inches']

and write

和写

window.onload=function(){
var obj={
 length:{
  inches:0.0254,
  miles:1609.344,
 }
}
var select1=document.createElement('select'),
select2=null,
f=document.createDocumentFragment(),
input=document.createElement('input'),
convert=document.createElement('button');
for(var a in obj.length){
 f.appendChild(document.createElement('option')).textContent=a;// easyway to access
}
select1.appendChild(f);
select2=select1.cloneNode(true);
input.type='text';
convert.textContent='Convert';

convert.addEventListener('click',function(e){
 console.log(
  input.value,
  obj.length[select1.textContent],// easyway to access
  obj.length[select2.textContent]// easyway to access
)
},false);

var bdy=document.body
bdy.appendChild(input);
bdy.appendChild(select1);
bdy.appendChild(select2);
bdy.appendChild(convert);
}