【问题标题】:Return Isn't Occuring In JavaScript forEach Function?JavaScript forEach 函数中不发生返回?
【发布时间】:2019-11-18 18:41:57
【问题描述】:

我正在使用 DocxJS 包来创建一个文档。在我的文档中,我正在尝试创建一个函数,该函数将迭代一些前端道具并为数组中的每个对象创建多个段落。每当我遍历数组并 console.log 它时,它都可以正常工作。当我做同样的事情并尝试使用 return 语句编写文档中所述的“新段落”时,它无法正常工作并且什么也不返回。

这里是包和文档: 包邮:https://www.npmjs.com/package/docx 文档:https://docx.js.org/#/

我的数组标记如下所示:

[{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?目前它只返回我请求的控制台日志。

【问题讨论】:

  • 想想return new Paragraph({里面发生了什么函数。并注意你是如何使用forEach,而不是map
  • .forEach() 方法忽略回调返回的值。
  • 你想做什么? .map() 将从现有数组的内容(由回调修改)创建一个新数组。
  • @Buckyx55 不,我的意思是return 没有在createItem 内部发生;它发生在您传递给 foreach 的 lambda 内部。您不能从内部函数内部的外部函数返回。我认为您可能想将foreach 更改为地图,然后执行return itemArray.map((item) =>...
  • 你想从createItem返回一个段落数组?那你要map

标签: javascript function return


【解决方案1】:

这样的东西应该适合你:

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')],[])

【讨论】:

  • 感谢您的回答。当我 console.log(paragraphArray) 时,它正确显示了数组中的段落,但是当我下载 word doc 时,它没有显示任何内容。创建paragraphArray之后,我是否必须以某种方式在文档标记中或在函数中返回数组?感谢您的帮助!
【解决方案2】:

Array.prototype.forEach() 总是返回 void 无论使用什么 Array.prototype.reduce() 或者编写一个自定义函数来接收一个数组并返回一个新数组(不是对你传递的那个的引用)你可以参考mdnMDN Array

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

   let resArr = itemArray.reduce((acc,item) =>{
             let para = new Paragraph({
                 text: `${item.description}`,
                 alignment: AlignmentType.CENTER,
                 style: 'educationDegree'
             })
             acc.push(para)
             return acc;
        },[]);

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

【讨论】:

  • 嘿抱歉没有看到是的,我打算使用 reduce 而不是 map 我正在浏览 cmets 并错过了,谢谢提醒我会更正它
猜你喜欢
  • 2017-05-02
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
相关资源
最近更新 更多