without changing your code too much:
$("nav ul > li > a ").on("click", function(){
$(this).siblings("ul").slideToggle();
$(this).toggleClass("active");
}
);
edit: You should be able to nest more lists using this code as well, considering it will always grab the sibling ul element if it exists. You may need to update your css however