使用直接数组JavaScript创建键/值哈希

时间:2014-04-15 11:45:59

标签: javascript arrays hash

我试图在javascript中用线性数组创建一个哈希。数组看起来像我想要创建键/值哈希

[13,0.011872336272725,13,0.01872336272725,13,0.0001,13,0.000660168379,13,0.006225,13,0.0000001,13,0.00925411794166,13,0.00000001,13,0.00093192461111,12,0.00007242822,12,0.9,13,0.000000005011872336272715,11,0.000001]

所以我想创建一个包含像

这样的值的哈希

days = {13:0.011872336272725,          13:.01872336272725,          12:0.00007242822,          11:0.000001}等

这样做我尝试这样

    for (var key in temphash) 
{
    var obj = temphash[key];
    for(var t = xValues.length; t >= 0; t--) 
    {
    if(obj[0] == xValues[t]) 
    {
        var keyy = xValues[t];
        if (!(keyy in days))
        {
        days[keyy] = new Array();
        }
        days[keyy].push(obj[1]); 
    }
    }
}

这个xValues包含一些值,我需要检查它是否存在于temphash中,然后只在几天内将其添加为键,其中包含所有键和值的最终哈希值。

我是JavaScript的新手,所以要管理它是否是愚蠢的问题..;)

2 个答案:

答案 0 :(得分:1)

JavaScript对象不能像您想要的输出示例那样具有重复的键。

选项1:创建一个按第一个数字分组的对象(例如13):

var arr = [13,0.011872336272725,13,0.01872336272725,13,0.0001,13,0.000660168379,13,0.006225,13,0.0000001,13,0.00925411794166,13,0.00000001,13,0.00093192461111,12,0.00007242822,12,0.9,13,0.000000005011872336272715,11,0.000001];
var result = {};

for(var i=0; i<arr.length; i++){
    if(i % 2 == 0){
        if(!(arr[i] in result)){
            result[arr[i]] = [];
        }
        result[arr[i]].push(arr[i+1]);
    }
}

输出:

{
    "11": [
        0.000001
    ],
    "12": [
        0.00007242822,
        0.9
    ],
    "13": [
        0.011872336272725,
        0.01872336272725,
        0.0001,
        0.000660168379,
    ]
    ...
}

然后你可以通过以下方式轻松获得所有13个:

console.log( result['13'] );

选项2:构建一个对象数组:

var arr = [13,0.011872336272725,13,0.01872336272725,13,0.0001,13,0.000660168379,13,0.006225,13,0.0000001,13,0.00925411794166,13,0.00000001,13,0.00093192461111,12,0.00007242822,12,0.9,13,0.000000005011872336272715,11,0.000001];

var result = [];
var obj;

for(var i=0; i<arr.length; i++){
    if(i % 2 == 0){
        obj = {}
        obj[arr[i]] = arr[i+1];
        result.push(obj);
    }
}

结果如下:

[
    {
        "13": 0.011872336272725
    },
    {
        "13": 0.01872336272725
    },
    {
        "13": 0.0001
    },
    ...
]

要查找给定键的所有值,您可以执行以下操作:

var target = 13; // search for what?
for(var j=0; j<result.length; j++){
    if(target in result[j]){
        console.log( 'found target, value is ' + result[j][target] );   
    }
}

答案 1 :(得分:0)

感谢@MrCode指导我让它发挥作用。上面的答案是正确的,但在创建JSON字符串时遇到的问题正是我想要的,所以我正在更新答案以供将来参考....

 function find(arr) {
    var result = {};

for(var key in arr){

    var obj = arr[key];
    if(!(obj[0] in result)){
        result[obj[0]] = [];
    }
    result[obj[0]].push(obj[1]);

}

}  这将最终创建像这样的东西

{
"11": [
    0.000001
],
"12": [
    0.00007242822,
    0.9
],
"13": [
    0.011872336272725,
    0.01872336272725,
    0.0001,
    0.000660168379,
]
...

} 虽然在上面的解决方案中,创建的json具有类似这样的关键&#34; 13,0.011872336272725&#34;之间

现在可以使用console.log(result [13])

等键轻松访问数据

我希望它有用;对某人有帮助