【问题标题】:Exporting map function into other file将地图功能导出到其他文件
【发布时间】:2020-04-26 12:20:54
【问题描述】:

是否可以将orders.map中的'order'函数导出并导入到另一个带有函数的JS文件中?我收到错误订单未定义。谢谢

main.js

     const getReport = async function() {
      const jsonlUrl = 'https://next.json-generator.com/api/json/get/xxxxx'
      const res = await fetch(jsonlUrl);
      const orders = await res.json();

      const orderlines = orders.map(order => ({

       order_id: order.order_id,

      customer_name: getName()

      }));

---

function.js

const getName = function() {
const customerName = order.customer_name
 return customerName
}

【问题讨论】:

  • 什么是“其他 JS 文件”?不清楚。
  • 你好@jhenderson2099,我已经更新了代码。谢谢
  • 已解决。我做了``` function.js const getName = function(order) { const customerName = order.customer_name return customerName } ``

标签: javascript node.js ecmascript-6 ecmascript-5


【解决方案1】:

已解决: 将参数传递给函数调用

main.js
     const getReport = async function() {
      const jsonlUrl = 'https://next.json-generator.com/api/json/get/xxxxx'
      const res = await fetch(jsonlUrl);
      const orders = await res.json();

      const orderlines = orders.map(order => ({

       order_id: order.order_id,

      customer_name: getName(order)

      }));

我在函数getName上加了参数

function.js
const getName = function(order) { 
const customerName = order.customer_name 
return customerName 
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2021-08-16
    • 2021-09-18
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    相关资源
    最近更新 更多