【问题标题】:Map an dynamic array in array js在数组js中映射一个动态数组
【发布时间】:2020-09-11 13:16:22
【问题描述】:

我有一个数组(它不是最终版本,数组是动态的)

[{
      "TemplateName": "C__01",
      "Template": "<div><h1>Hello world!</h1><div id='C__01__body'></div></div>",
      "TemplateBody": "body",
      "TemplateChildren": [{
        "TemplateName": "C__02",
        "Template": "<p>Hello1</p>",
        "TemplateBody": null,
        "TemplateChildren": [{
          "TemplateName": "C__03",
          "Template": "<p>Hello2</p>",
          "TemplateBody": null,
          "TemplateChildren": [{
            "TemplateName": "C__04",
            "Template": "<p>Hello3</p>",
            "TemplateBody": null,
            "TemplateChildren": null
          }, {
            "TemplateName": "C__05",
            "Template": "<p>Hello4</p>",
            "TemplateBody": null,
            "TemplateChildren": null
          }]
        }]
      }]
    }];

我不是在找字段,我只需要在数组中映射每个数组

【问题讨论】:

  • 映射数组中的每个数组是什么意思?你选择了什么方法?
  • 我想为数组中的每个数组做类似array.map() 的事情
  • 你能提供你需要的样本输出吗?
  • 你好。我不需要输出。我需要更改 innerHTML 以使用数组中的字段值。因为我需要为数组中的每个数组做array.map()

标签: javascript arrays web frontend web-frontend


【解决方案1】:
const parse = arr => {
      arr.map(item => {
        console.log(item);
        if (item.TemplateChildren) {
          parse(item.TemplateChildren);
        } else {
          return [];
        }
      });
    }

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多