保存数据为对象或数组? [英] saving data as object or array?

查看:109
本文介绍了保存数据为对象或数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

4 0.128 0.039
5 0.111 0.037
6 0.095 0.036

我需要通过公知的第一值,以获得第二个和第三个值。
如果我有 4 的价值我想回去两个变量: A = 0.111 b = 0.037

I need to get the second and third value by a known first value. If I have a value of 4 I want to get back two variables: a = 0.111 and b = 0.037

什么是最好的变量类型存储上述获得简单的访问数据的数据?对象或多维数组?

What would be the best variable type for storing the data shown above to get simple access to the data? An object or an multidimensional array?

推荐答案

有关四通八达的交通网络,我会包含数组对象去

For ease of access, I'd go with an object containing arrays:

{
    '4': [ 0.128, 0.039 ],
    '5': [ 0.111, 0.037 ],
    ... 
}

超过阵列使用对象的第二个原因是方便迭代。想象一下:

A second reason for the use of objects over arrays is ease of iteration. Imagine this:

var myData = [];
myData[4]  = [ 0.128, 0.039 ];
myData[10] = [ 42, 23 ];

for (var i = 0; i < myData.length; i++)
{
    console.log(myData[i]);
}

会给你

null
null
null
null
[ 0.128, 0.039 ]
null
null
null
null
null
[ 42, 23 ]

...这可能不是你想要的东西;)

... which is probably not what you want ;)

这篇关于保存数据为对象或数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆