Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回Isn';JavaScript forEach函数中是否出现了错误?_Javascript_Function_Return - Fatal编程技术网

返回Isn';JavaScript forEach函数中是否出现了错误?

返回Isn';JavaScript forEach函数中是否出现了错误?,javascript,function,return,Javascript,Function,Return,我正在使用DocxJS包创建一个文档。在我的文档中,我试图创建一个函数,该函数将迭代一些前端道具,并为数组中的每个对象创建多个段落。每当我迭代数组和console.log时,它都正常工作。当我做同样的事情,并试图使用return语句来编写文档中所述的“新段落”时,它工作不正常,不返回任何内容 以下是软件包和文档: 包裹: 文件: 我的数组标记如下所示: [{item}, {item}, {item}] 阵列中的对象包括: {item.value, item.description, item.

我正在使用DocxJS包创建一个文档。在我的文档中,我试图创建一个函数,该函数将迭代一些前端道具,并为数组中的每个对象创建多个段落。每当我迭代数组和console.log时,它都正常工作。当我做同样的事情,并试图使用return语句来编写文档中所述的“新段落”时,它工作不正常,不返回任何内容

以下是软件包和文档: 包裹: 文件:

我的数组标记如下所示:

[{item}, {item}, {item}]
阵列中的对象包括:

{item.value, item.description, item.tags}
以下是我创建的函数:

const createItem = () => {
    let itemArray = this.props.goods.products.items;
    console.log(itemArray);

    // This will properly console.log the item description for each one (3 total)
    itemArray.forEach((item) => {
        console.log(item.description);
    })

    // This doesn't return anything, but the console.log DOES return the item descriptions like before
    itemArray.forEach((item) => {
         return new Paragraph({
             text: `${item.description}`,
             alignment: AlignmentType.CENTER,
             style: 'educationDegree'
         }, console.log(item.description));
    })
}
稍后在我用docx包创建的文档中,我只调用如下函数:createItem()

我试着传入createItem(this.props.goods.products.items),但它什么也没做。我尝试过这样做:this.createItem(this.props.goods.products.items),它只是返回this.createItem不是函数

我就是不明白为什么它不能正常工作,并在遍历数组时返回每个项的item.description?目前,它只返回我请求的控制台日志。

Array.prototype.forEach()总是返回void,无论使用Array.prototype.reduce()还是编写一个自定义函数,接收数组并返回一个新数组(不是对您传递的数组的引用),您都可以引用mdn

您可以看到文档中提到的forEach的返回值总是未定义的,我建议您使用Array.prototype.reduce()

获取新对象是一种很好的做法,而不是改变现有的引用对象,因此认为这会有所帮助。

您可以看到文档中提到的forEach的返回值总是未定义的,我建议您使用Array.prototype.reduce()


获取新对象而不是修改现有的引用对象是一种很好的做法,因此我认为这会有所帮助。

类似的方法应该适合您:

const itemArray = [{description: 'Lorem ipsum'}, {description: 'Lorem ipsum'}]

const AlignmentType = {
   CENTER: 'center'
}

class Paragraph {
  constructor(text, alignment, style) {
    this.text = text
      this.alignment = alignment
      this.style = style
  }
}

const paragraphArray = itemArray.reduce((acc, item) => [...acc, new Paragraph(item.description, AlignmentType.CENTER, 'educationDegree')],[])

像这样的东西应该适合你:

const itemArray = [{description: 'Lorem ipsum'}, {description: 'Lorem ipsum'}]

const AlignmentType = {
   CENTER: 'center'
}

class Paragraph {
  constructor(text, alignment, style) {
    this.text = text
      this.alignment = alignment
      this.style = style
  }
}

const paragraphArray = itemArray.reduce((acc, item) => [...acc, new Paragraph(item.description, AlignmentType.CENTER, 'educationDegree')],[])

想一想什么函数
返回新段落({
在里面发生。注意你是如何使用
forEach
,而不是
map
.forEach()
方法忽略回调返回的值。你想做什么?
.map()
将从内容中创建一个新数组(由回调修改)“Buckyx55不,我的意思是,
返回
不是发生在
createItem
内部;而是发生在传递给
foreach
的lambda内部。不能从内部函数中的外部函数返回。我想您可能需要将
foreach
更改为映射,然后执行
。”>return itemArray.map((item)=>…
并从
createItem
返回段落数组?然后您需要
map
。思考
返回新段落({
的函数是什么。注意如何使用
forEach
,而不是
map
.forEach()
方法忽略回调返回的值。您试图做什么?
.map()
将从内容(由回调修改)创建一个新数组“Buckyx55不,我的意思是,
返回
不是发生在
createItem
内部;而是发生在传递给
foreach
的lambda内部。不能从内部函数中的外部函数返回。我想您可能需要将
foreach
更改为映射,然后执行
。”>返回itemArray.map((项)=>…
然后从
createItem
返回一个段落数组?然后你想要
map
。嘿,对不起,没有看到是的,我想使用reduce not map我正在浏览评论,没有看到,谢谢你提醒我,我会更正的。对不起,没有看到是的,我想使用reduce not map尽管有这些评论,但没有提到,谢谢你提醒我,我会改正的,谢谢你的回答,它正确地显示了数组中的段落,但是当我下载word doc时,它没有显示任何内容。我是否必须以某种方式在文档标记中返回数组,或者在创建paragraphArray后在函数中返回数组?感谢您的帮助!感谢您的回答。当我使用console.log(paragraphArray)时,它正确地显示了数组中的段落,但当我下载word文档时,它没有显示任何内容。我是否必须以某种方式在文档标记中或在创建段落数组后在函数中返回数组?感谢您的帮助!