I'm trying to understand how this snippet of codes work to flatten multi levels/nested array
can anyone please help to elaborate? thanks
var flatten = function(a, shallow,r){
if(!r){ r = []} //what does the exclamation mark mean here?...
if (shallow) {
return r.concat.apply(r,a); //I can't find what's .apply for concat method
}
for(var i=0; i<a.length; i++){
if(a[i].constructor == Array){
flatten(a[i],shallow,r);
}else{
r.push(a[i]);
}
}
return r;
}
alert(flatten([1, [2], [3, [[4]]],[5,6]]));