递归添加承诺等待在打字稿中的记录密钥
我有记录类型别名schemaobj
const schemaObj: Record<string, any> = {};
我有一系列字符串ref = [“ abc.json”,“ test.json”]
我在ref
ref /code>,对于单个元素,调用递归功能和试图分配给相同record
键>键
的返回。这就是我
if (ref.length > 0) {
schemaObj[key] = await Promise.all(
_.map(ref, async (o) => {
return await recursiveFunction(o);
}).flat()
).then((values) => {
return values;
});
}
这样做的目的,但我更愿意以链序列(以更好地可读性和更清洁的代码)来完成。尤其是在我正在创建ref
的同一链序列的顶部,
refSchemas = [{"values":"abc.json"},{"values": "xyz"},{ "values": "test.json"}]
_.filter(refSchemas, (o) => o.values.includes(".json"))
.map((e) => e.values)
.map(async (e:string) => {
schemaObj[key] = await recursiveFunction(e);
});
但这覆盖了schemaobj [key]
value。我尝试了差异符号...
,但没有帮助。
I have record type alias schemaObj
const schemaObj: Record<string, any> = {};
I have a array of strings ref = ["abc.json", "test.json"]
I am iterating over ref
and for individual element calling a recursive function and the return trying to assign to same record
key
. This is what I am doing
if (ref.length > 0) {
schemaObj[key] = await Promise.all(
_.map(ref, async (o) => {
return await recursiveFunction(o);
}).flat()
).then((values) => {
return values;
});
}
This serves the purpose, but I was more willing to do it in a chain sequence ( for better readability and cleaner code ). Especially on top of the same chain sequence where I am creating ref
something like below
refSchemas = [{"values":"abc.json"},{"values": "xyz"},{ "values": "test.json"}]
_.filter(refSchemas, (o) => o.values.includes(".json"))
.map((e) => e.values)
.map(async (e:string) => {
schemaObj[key] = await recursiveFunction(e);
});
But this overrides the schemaObj[key]
value. I tried the spread notation ...
but didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回循环中的值,但您不返回它们。而不是这些,您可以在
MAP 的每一轮中,将值分配回
schemaobj [key]
可能的修复程序可能是
您也可以使用直接
filter> filter 代替图书馆的过滤器的过滤器的过滤器的过滤器
_。过滤器
map
returns values in a loop, but you don't return them. Instead of that, you assign values back toschemaObj[key]
in every round ofmap
A possible fix could be
You also can use a direct
filter
instead of a library's filter_.filter