Hide WordPress menu items if page is set to private

User the following code if you want to hide a menu item whose page is set to private.

add_filter ('wp_nav_menu_objects', 'jj_filter_private_pages_from_menu', 10, 2); 
function jj_filter_private_pages_from_menu ($items, $args) {
foreach ($items as $ix => $obj) {
if (!is_user_logged_in () && 'private' == get_post_status ($obj->object_id)) {
unset ($items[$ix]);
}
}
return $items;
}