将地图功能导出到其他文件中

问题描述 投票:0回答:1

是否可以在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
}

javascript node.js ecmascript-6 ecmascript-5
1个回答
0
投票

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

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 
} 
© www.soinside.com 2019 - 2024. All rights reserved.