I know this code is correct, I did not write it. I am trying to understand what the code is doing.
- create variable
- loop through unordered list’s children
- set cssClass = class name
- if the cssClass is not in myarray, add it to myarray
I am familiar with other ways to check to see if an item is in an array, and familiar with other ways to add an item to an array, but this method confuses me. Do other languages support this type of syntax?
var myarray = [];
$(".somelist > li").each(function() {
var cssClass = $(this).attr("class");
//This part below is CONFUSING
if(!myarray[cssClass]) {
myarray[cssClass] = true;
}
});
Here’s a brief outline of what’s happening, the
myarrayvariable is used as a hash –1 – Checks to see if the current class name is in the array.
myarray[cssClass]will return a ‘falsy‘ value if the class name is not conatined in the array.2 – If the class is not in the array, add it to the array and set it’s value to true