递归添加承诺等待在打字稿中的记录密钥

发布于 2025-01-21 06:11:46 字数 1036 浏览 3 评论 0原文

我有记录类型别名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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2025-01-28 06:11:46

返回循环中的值,但您不返回它们。而不是这些,您可以在 MAP 的每一轮中,将值分配回schemaobj [key]

可能的修复程序可能是

schemaObj[key] = _.filter(refSchemas, (o) => o.values.includes(".json"))
        .map((e) => e.values)
        .map(async (e:string) => {
           return await recursiveFunction(e); //return a value
        });

您也可以使用直接 filter> filter 代替图书馆的过滤器的过滤器的过滤器的过滤器_。过滤器

schemaObj[key] = refSchemas.filter((o) => o.values.includes(".json"))
        .map((e) => e.values)
        .map(async (e:string) => {
           return await recursiveFunction(e); //return a value
        });

map returns values in a loop, but you don't return them. Instead of that, you assign values back to schemaObj[key] in every round of map

A possible fix could be

schemaObj[key] = _.filter(refSchemas, (o) => o.values.includes(".json"))
        .map((e) => e.values)
        .map(async (e:string) => {
           return await recursiveFunction(e); //return a value
        });

You also can use a direct filter instead of a library's filter _.filter

schemaObj[key] = refSchemas.filter((o) => o.values.includes(".json"))
        .map((e) => e.values)
        .map(async (e:string) => {
           return await recursiveFunction(e); //return a value
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文