我在下面有两个函数(它们是从一个更大的脚本中取出的,所以假设所有内容都已定义等等。self.sentenceObjs
效果很好。它返回一个完全像它应该做的对象。self.parseBodySections
出于某种原因,设置bodyJSON
为一个数组,undefined
即使self.sentenceObjs
返回给定的完美对象我要映射的dom
对象数组。由于某种原因,当我运行dom.map(self.sentenceObjs)
它时,它会为每个对象返回未定义。知道为什么会这样吗?Array.map()
我错过了什么吗?
self.parseBodySections = function(dom, cb) {
var bodyJSON = dom.map(self.sentenceObjs);
console.log(bodyJSON); // prints: [ undefined, undefined, undefined, undefined, undefined ]
return cb(null, bodyJSON);
};
self.sentenceObjs = function(section) {
var paragraphToTextAndLinks = function(cb) {
return self.paragraphToTextAndLinks(section.children, function(err, paragraphText, links) {
if (err) {
return cb(err);
}
return cb(null, paragraphText, links);
});
};
return async.waterfall([
paragraphToTextAndLinks,
self.paragraphToSentences
],
function(err, sentences, paragraphPlaintext) {
var paragraph = {
type: section.name,
value: paragraphPlaintext,
children: sentences
};
console.log(paragraph) // prints perfect object (too long to show here)
return paragraph;
});
};