0

I am trying to create a hash out of linear array in javascript. The array looks like this from which I want to create key/value hash

[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]

so I want to create a hash that will contain values like this

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

to do so I trying like this

    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]); 
    }
    }
}

This xValues contains some values that I need to check if exist in temphash then only add it as key in days which will have final hash with all key and values in it.

I am pretty new in JavaScript so manage if it is silly question..;)

2
  • Your desired result is not possible because you have duplicate keys in the object. Unless you want the result as a string... Commented Apr 15, 2014 at 11:49
  • hmm, but is there any other convenient way to do it..like to get all values of one key on another array...or soemthing Commented Apr 15, 2014 at 11:50

3 Answers 3

1

JavaScript objects can't have duplicate keys like in your desired output example.

Option 1: create an object that groups by the first number (e.g 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]);
    }
}

Outputs:

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

You can then easily get all 13's by doing:

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

Option 2: build an array of objects instead:

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);
    }
}

The result looks like:

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

To find all values for a given key, you can do:

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] );   
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

so if I want to access all values for say "13" I need loop for just arr[13] wouild work...
@user1618 added another method if you want to group them.
Thanks, I just try and understand it, will accept the answer soon
it is creating wrong JSON string in second case which is like this [ { "13,0.0000005011872336272725":[ 13, "0.0000005011872336272725" ] }, { "13,0.0000001":[ 13, "0.0000001" ] }, { "13,0.0000005011872336272725":[ 13, "0.0000005011872336272725" ] },
@user1618 which method are you using to get that? Can you create a jsfiddle to demo it?
|
0

You can try this format for a key/(array values) result:

var i, key, value, obj;
for (i = 0; i < array.length; i += 2) {
    key = array[i];
    value = array[i+1];
    if (!obj[key]) {
        obj[key] = [];
    }
    obj[key].push(value);
}

result:

{
    13: [0.011872336272725, 0.01872336272725, ...],
    12: [0.00007242822, ...],
    ...
}

You should be careful about the array length and add some checks for the integrity of the data.

Comments

0

Thanks to @MrCode for guiding me to make it work. The answers above are correct but minor issue in creating JSON string exactly what I want, so I am updating the answer for future references....

 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]);

}

} This will end up creating something like this

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

} While in above solutions the created json was having key something like this "13,0.011872336272725" in between

Now data can be accessed easily with key such as console.log(result[13])

I hope It woul;d be helpful for someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.