When dealing with a recursive call within an array object, where I'm passing an array to a function and each proceeding call, I'd like the array argument to be n-1.
I normally use:
- Array.prototype.shift
- Array.prototype.pop
I have to call these methods the line before the recursive call. Check the example below.
Here is what I normally resort to:
function responseData(urls){
if (urls.length-1){
return;
}
http.get(urls[0], function(res) {
res.setEncoding('utf8');
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log(body.toString());
urls.shift(); // NOTICE THIS LINE
printResponseData(urls);
});
});
}
Is there a cleaner alternative to this approach?
Can I eliminate using shift or pop that return values, to a method that will minimize the size of the array and return the newly minified array so I can pass this straight to my recursive call?
printResponseData(urls.slice(1));if (urls.length-1){is weird. Did you meanif (urls.length==0){?