为什么你不能将对象返回给Javascript的Array.map()并且它正确映射? [英] Why can you not return an object to Javascript's Array.map() and it map it properly?
问题描述
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 pa ragraphToTextAndLinks = function(cb){
return self.paragraphToTextAndLinks(section.children,function(err,paragraphText,links){
if(err){
return cb(err);
}
return cb(null,paragraphText,links);
});
};
返回async.waterfall([
paragraphToTextAndLinks,
self.paragraphToSentences
],
函数(err,sentence,paragraphPlaintext){
var paragraph = {
type:section.name,
value:paragraphPlaintext,
children:sentence
};
console.log(paragraph)/ /打印完美的对象(在这里显示太长)
return paragraph;
});
};
问题是,你返回段落瀑布的回调功能。
所以函数sentenceObjs不返回任何东西,或者是未定义。
你需要将一个回调函数传递给sentenceObjs,并调用async.map而不是Array.map:
self.parseBodySections = function(dom,cb){
async.map(dom,self.sentenceObjs,function (err,bodyJSON){
console.log(bodyJSON); // prints:[undefined,undefined,undefined,undefined,undefined]
return cb(null,bodyJSON);
}) ;
};
self.sentenceObjs = function(section,cb){
var paragraphToTextAndLinks = function(cb){
return self.paragraphToTextAndLinks(section.children,function(err,paragraphText,链接){
if(err){
return cb(err);
}
return cb(null,paragraphText,links);
} );
};
返回async.waterfall([
paragraphToTextAndLinks,
self.paragraphToSentences
],
函数(err,sentence,paragraphPlaintext){
var paragraph = {
type:section.name,
value:paragraphPlaintext,
children:sentence
};
console.log(paragraph); //打印完美的对象(在这里显示的时间太长)
return cb(null,paragraph);
});
};
I have two functions below (they are taken out of a larger script so assume everything is defined etc. self.sentenceObjs
works great. It returns an object exactly like it's supposed to do. self.parseBodySections
for some reason sets bodyJSON
to an array of undefined
even though self.sentenceObjs
is returning perfect objects given the dom
array of objects I want mapped. For some reason when I run dom.map(self.sentenceObjs)
it returns undefined for each object. Any idea why this would be? Is there something with Array.map()
that I missing?
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;
});
};
The problem is, that you return "paragraph" in the callback function of waterfall. So the function sentenceObjs returns nothing, or undefined.
You need to pass in a callback function to sentenceObjs and call async.map instead of Array.map:
self.parseBodySections = function(dom, cb) {
async.map(dom, self.sentenceObjs, function(err, bodyJSON) {
console.log(bodyJSON); // prints: [ undefined, undefined, undefined, undefined, undefined ]
return cb(null, bodyJSON);
});
};
self.sentenceObjs = function(section, cb) {
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 cb(null, paragraph);
});
};
这篇关于为什么你不能将对象返回给Javascript的Array.map()并且它正确映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!