是否可以在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
}
已解决:将参数传递到函数调用中
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
}