if you change sections to be array instead of object you can do something like this
$(function(){
var sections = [],//change to array []
_width = $(window).width();
$('.section').each(function(){
sections.push($(this).offset().left);//you can use push
});
var $document=$(document);//you can create a variable outside the scroll event
$document.scroll(function(){
var pos = $document.scrollLeft();
$.each(sections,function(i,n){// loop for each section
if(n >= pos && n <= pos + _width){
$('a').removeClass('active');
//change #nav_ for #nav_section
//add 1 to i as you start with nav_section1
$('#nav_section' +(i+1)).addClass('active');
}
});
});
});