Javascript 映射内的递归函数未定义
我有以下资料:Javascript 映射内的递归函数未定义,javascript,Javascript,我有以下资料: export const normaliseHistoryLog = history => { if (history.children.length === 0) { debugger; return { id: history.id, date: history.timestamp, time: history.timestamp,
export const normaliseHistoryLog = history => {
if (history.children.length === 0) {
debugger;
return {
id: history.id,
date: history.timestamp,
time: history.timestamp,
event: history.event.label,
eventHTML: getHTML(history.event.detail),
source: history.source,
requestor: history.requestor
}
}
debugger;
return {
id: history.id,
date: history.timestamp,
detail: history.children.map(normaliseHistoryLog),
time: history.timestamp,
event: history.event.label,
eventHTML: getHTML(history.event.detail),
source: history.source,
requestor: history.requestor
}
}
我有一个错误
detail: history.children.map(normaliseHistoryLog),
说“normaliseHistoryLog”是未定义的
我尝试了以下方法:
detail: history.children.map((el) => normaliseHistoryLog(el))
但还是没有运气
该函数称为:
export const getHistory = asyncMiddleware(async (req) => {
const path = `url`;
const response = await get(req, path);
debugger;
return response.result.map(normaliseHistoryLog);
});
其中response.result是对象数组TL;博士强>
function normaliseHistoryLog(history) {
if (history.children.length === 0) {
debugger;
return {
id: history.id,
date: history.timestamp,
time: history.timestamp,
event: history.event.label,
eventHTML: getHTML(history.event.detail),
source: history.source,
requestor: history.requestor
}
}
debugger;
return {
id: history.id,
date: history.timestamp,
detail: history.children.map(normaliseHistoryLog),
time: history.timestamp,
event: history.event.label,
eventHTML: getHTML(history.event.detail),
source: history.source,
requestor: history.requestor
}}
export function normaliseHistoryLog;
这是由于吊装造成的。
声明函数normaliseHistoryLog
的方式称为函数表达式。
函数表达式(基本上是将函数指定为变量)即使导出,也会在稍后阶段完成。
这意味着在创建闭包时(当您声明函数并分配它时),函数是未知的(可用的)
您可以通过在JS中查找提升来了解更多信息。
例如:
请添加一些示例数据和一个示例,说明如何将函数调用到代码中,以将其转换为完整的函数。您可以使用“实时演示”功能,这样人们就可以在问题本身中运行代码。@昆汀,我认为OP是在尝试递归映射,也就是说,他们将函数本身作为回调传递。@Li357-是的,看起来是这样。只要看一眼代码,它看起来应该可以工作,所以我想要一个可以运行的版本,看看会发生什么。