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.

1
  • 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. Commented Jun 7, 2013 at 16:34

2 Answers 2

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

9 Comments

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.
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.
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).
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: To learn more about objects and how to access them, have a look at the link I already referred you to: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…. It has lots of examples.
|
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

2 Comments

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.
dc5's edit shows how to do this. The second answer here ... stackoverflow.com/questions/18912/how-to-find-keys-of-a-hash also shows an alternative way of doing this.

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.