0

I am trying to build a data structure.

In my limited knowledge, 'hash table' seems to be the way to go. If you think there is an easier way, please suggest it.

I have two, 1-dimensional arrays:-

A[] - contains names of badges (accomplishment)

B[] - contains respective dates those achievements were accomplished from array A[].

An achievement/accomplishment/badge can be accomplished more than one time.

Therefore a sample of the two arrays:-

A['scholar', 'contributor', 'teacher', 'student', 'tumbleweed', 'scholar'.....,'scholar',......]

B['1/2010', '2/2011', '3/2011', '6/2012', '10/2012', '2/2013',......'3/2013',........]

What I want to achieve with my data structure is:-

A list of unique keys (eq:- 'scholar') and all of its existing values (dates in array B[]).

Therefore my final result should be like:-

({'scholar': '1/2010', '2/2013', '3/2013'}), ({'contributor' : ........})..........

This way I can pick out a unique key and then traverse through all its unique values and then use them to plot on x-y grid. (y axis labels being unique badge names, and x axis being dates, sort of a timeline.)

Can anyone guide me how to build such a data structure??

and how do I access the keys from the data structure created.... granted that I don't know how many keys there are and what are their individual values. Assigning of these keys are dynamic, so the number and their names vary.

Philo
  • 1,931
  • 12
  • 39
  • 77
  • 1
    Since you seem to only have string keys, you can use plain objects as hash-tables. Have a look at [MDN - Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects). – Felix Kling Jun 07 '13 at 16:34

2 Answers2

1

Your final object structure would look like this:

{
    'scholar': [],
    'contributor': []
}

To build this, iterate through the names array and build the final result as you go: if the final result contains the key, push the corresponding date on to its value otherwise set a new key to an array containing its corresponding date.

something like:

var resultVal = {};
for(var i = 0; i < names.length; ++i) {
    if(resultVal[names[i]]) {
        resultVal[names[i]].push(dates[i]);
    } else {
        resultVal[names[i]] = [dates[i]];
    }
}

Accessing the result - iterating through all values:

for(var key in resultVal) {
    var dates = resultVal[key];

    for(var i = 0; i < dates.length; ++i) {
       // you logic here for each date
       console.log("resultVal[" + key + "] ==> " + resultVal[key][i]);
    }
}

will give results like:

resultVal[scholar] ==> 1/2010
resultVal[scholar] ==> 2/2013
resultVal[scholar] ==> 3/2013
resultVal[contributor] ==> 2/2011
resultVal[teacher] ==> 3/2011
resultVal[student] ==> 6/2012
resultVal[tumbleweed] ==> 10/2012
dc5
  • 12,341
  • 2
  • 35
  • 47
  • and then how do i access each individual key? Note: I don't know how many keys there are or even their unique names. there could a,b,x,y,z or a,b,c,d,e,f,g. for names of badges. – Philo Jun 07 '13 at 17:28
  • is key a reserved word then? Because like I said, I don't know the individual values of each key... key values are assigned dynamically. currently when I put a breakpoint, on the resultVal {}. I can see it has dates, but i can't see the names...its in the {object},{array} format. – Philo Jun 07 '13 at 17:40
  • 1
    key isn't a keyword - it's just a variable. The construct for(foo in bar) will iterate through all the keys of an object (the ones that can be iterated anyway). – dc5 Jun 07 '13 at 17:43
  • one problem:- when I do, dates.length instead of 16 dates for my first badge, I get 361..... although dates show that it has 16 elements....haha. – Philo Jun 07 '13 at 17:55
  • Also how can i get the individual names of each key? right now my key names show up as '1' '2' '3' '4' 'a' 'x' '-'.... these are not the correct key names... – Philo Jun 07 '13 at 18:12
  • There's probably a mistake in the code (or assumptions) somewhere. When I run the logic above with two equal length arrays and log the results I see something like the above (added results to answer) – dc5 Jun 07 '13 at 18:18
  • so i was looking through the hash table.... and I see that, when doing the following resultVal[names[i]].push(dates[i]); it only picks out the first letter of each name. – Philo Jun 07 '13 at 18:26
  • ah never mind, instead of using names... i did a split on my initial array... the names of each achievement was stored with a '*' separating each. thanks. works perfectly. – Philo Jun 07 '13 at 18:31
  • 1
    @Philo: To learn more about objects and how to access them, have a look at the link I already referred you to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects. It has lots of examples. – Felix Kling Jun 07 '13 at 22:11
0

You can try this...

var A = ['scholar', 'contributor',
  'teacher', 'student', 'tumbleweed', 'scholar','scholar'];

var B = ['1/2010', '2/2011', 
  '3/2011', '6/2012', '10/2012', '2/2013','3/2013'];

var combined = {};

for(var i=0;i<A.length;i++) {
  if(combined[A[i]] === undefined) {
    combined[A[i]] = [];
  }
  combined[A[i]].push(B[i]);
}

Then each one of the arrays in combined can be accessed via

combined.scholar[0]

or

combined['scholar'][0]

Note the === when comparing against undefined

Gordolio
  • 1,925
  • 15
  • 22
  • Except, I don't know all the names of the badges ('scholar'). so I cannot access like combined['scholar']. I will need to access as: Combined [keyof ith position], give me its 0...n values. – Philo Jun 07 '13 at 17:24
  • dc5's edit shows how to do this. The second answer here ... http://stackoverflow.com/questions/18912/how-to-find-keys-of-a-hash also shows an alternative way of doing this. – Gordolio Jun 07 '13 at 23:03