Reputation: 5442
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..;)
Upvotes: 0
Views: 87
Reputation: 5442
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
Upvotes: 0
Reputation: 516
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.
Upvotes: 0
Reputation: 64526
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] );
}
}
Upvotes: 1