Skip to content

Instantly share code, notes, and snippets.

@pgainda
Created June 10, 2013 19:40
Show Gist options
  • Select an option

  • Save pgainda/5751592 to your computer and use it in GitHub Desktop.

Select an option

Save pgainda/5751592 to your computer and use it in GitHub Desktop.
Associative arrays in javascript look like this: var myAssoc = {"key1":"val1","key2":"val2"} To get a value, given a key, you would write: myAssoc[key] So what are associative arrays? The short answer is that they are lists of key, value pairs. In javascript, everything is really just an object. So another way to put it is that they are objects …
//students on the roll
var roll = ["robert","joe","sharon"];
//students actually in the class
var students = {"sharon":true, "robert":true};
//Loop over the roll array. For each name in the roll array, check if
//that name exists in the students associate array.
//Whenever you find a student that is present, increment the numPresent
//counter by 1
var numPresent = 0;
for (i=0;i<roll.length;i++){
if(students[roll[i]]===true){
numPresent++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment