我正在使用Node.js和cheerio来抓取一些网址,我想查看网址是否包含某些元素。如果他们满足条件我想将它们保留在数组中,但如果它们不存在,我想删除它们。
function runRequest(url, cb) {
request(url, function(error, response, html) {
if (!error) {
//load the cheerio object
var $ = cheerio.load(html);
if (/*some condition isn't satisfied*/) {
//i can only return null
cb(null, null);
} else {
//keep url in the array
cb(null, url);
}
}
});
}
async.map(urls, runRequest, function (err, results) {
//results should now only contain some of the urls if the urls didn't satisfy the condition
console.log(results);
});
虽然使用null作为参数触发回调是一个可靠的解决方法,我想知道是否有一种方法可以触发回调但基本上从数组中删除元素,因为将其设为null会导致其他问题。 (如果我不在async.map中的第二个参数上触发回调,则第三个参数甚至不会运行。)