【问题标题】:Javascript: Loop through nested objects and arraysJavascript:遍历嵌套对象和数组
【发布时间】:2018-08-28 05:57:23
【问题描述】:

我有一个服务器响应,其中包含您在代码 n-p 中看到的结构的数据。 我的目标是遍历每个同意并添加渠道

来自响应的数据:

[
  {
      "consents": [
          {
              "channels": [
                  {
                      "granted": true,
                      "id": "sms",
                      "title": "SMS/MMS"
                  },
                  {
                      "granted": true,
                      "id": "email",
                      "title": "E-Mail"
                  },
                  {
                      "granted": false,
                      "id": "phone",
                      "title": "Telefon"
                  },
                  {
                      "granted": false,
                      "id": "letter",
                      "title": "Brief"
                  }
              ],
              "client": "App",
              "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d",
              "date": "2018-03-08T16:03:25.753Z",
              "granted": true,
              "name": "bestandsdaten-alle-produkte",
              "version": "1.0.0",
              "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646"
          }
      ],
      "createdAt": "2018-03-08T16:03:25.778Z",
      "id": "1b9649a6-d8de-45c6-a0ae-a03cecf71cb5",
      "updatedAt": "2018-03-08T16:03:25.778Z",
      "username": "demo-app"
  },
  {
      "consents": [
          {
              "channels": [
                  {
                      "granted": true,
                      "id": "sms",
                      "title": "SMS/MMS"
                  },
                  {
                      "granted": true,
                      "id": "email",
                      "title": "E-Mail"
                  },
                  {
                      "granted": true,
                      "id": "phone",
                      "title": "Telefon"
                  },
                  {
                      "granted": true,
                      "id": "letter",
                      "title": "Brief"
                  }
              ],
              "client": "App",
              "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d",
              "date": "2018-03-08T14:51:52.188Z",
              "granted": true,
              "name": "bestandsdaten-alle-produkte",
              "version": "1.0.0",
              "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646"
          }
      ],
      "createdAt": "2018-03-08T14:51:52.208Z",
      "id": "cf550425-990e-45ef-aaee-eced95d8fa08",
      "updatedAt": "2018-03-08T14:51:52.208Z",
      "username": "demo-app"
  },
  {
      "consents": [
          {
              "channels": [
                  {
                      "granted": false,
                      "id": "sms",
                      "title": "SMS/MMS"
                  },
                  {
                      "granted": true,
                      "id": "email",
                      "title": "E-Mail"
                  },
                  {
                      "granted": true,
                      "id": "phone",
                      "title": "Telefon"
                  },
                  {
                      "granted": false,
                      "id": "letter",
                      "title": "Brief"
                  }
              ],
              "client": "App",
              "configId": "99df8e86-2e24-4974-80da-74f901ba6a0d",
              "date": "2018-03-08T14:48:27.024Z",
              "granted": true,
              "name": "bestandsdaten-alle-produkte",
              "version": "1.0.0",
              "versionId": "bd002dcd-fee6-42f8-aafe-22d0209a0646"
          }
      ],
      "createdAt": "2018-03-08T14:48:27.054Z",
      "id": "7fc1f087-2139-4494-bad7-161b0c6231a9",
      "updatedAt": "2018-03-08T14:48:27.054Z",
      "username": "demo-app"
      },
    ]

我走的路如下。但我需要一种将频道附加到每个同意书的方法。有没有办法解决这个问题?

consentsList.forEach((consent, index, array) => {
      consent.consents.forEach((c) => {
        Object.keys(c).forEach((key) => {
          dd.content.push(
            {
              columns: [
                {
                  text: `${key}: `, style: 'text'
                },
                {
                  text: c[key], style: 'text'
                }
              ]
            }
          );
        });
      });
      Consents.push(consent);
      if (index !== array.length - 1) {
        dd.content.push({
          margin: [0, 0, 0, 5],
          canvas: [{
            type: 'line', x1: 0, y1: 5, x2: 595 - (2 * 40), y2: 5, lineWidth: 0.6
          }]
        });
      }
    });

我想输出单个频道,其中频道位于每个条目的顶部

【问题讨论】:

  • 这是什么dd.content
  • dd.content 来自 pdfmake 它是数组,我需要它来生成 pdf
  • 预期输出是什么?
  • 我在帖子中添加了一张图片,看看要输出什么。

标签: javascript arrays object for-loop foreach


【解决方案1】:

您可以使用地图提取标题。并像这样使用 join 连接它们。

       let value = "";
       if(key === "channels"){
          value = c[key].map(x => x.title).join(" ,");
       }      
       else {
          value = c[key];
       }
       columns: [
                {
                  text: `${key}: `, style: 'text'
                },
                {
                  text: value, style: 'text'
                }
       ]

【讨论】:

    【解决方案2】:

    我已经写了一段代码,希望这是你要找的。未优化,请随意优化

    var a = responseData;
    a.forEach(function(items) {
      var allChannels = [];
      items.consents.forEach(function(innerItems){
         innerItems.channels.forEach(function(value){
            allChannels.push(value.title);        
         })
         items.allChannels = allChannels.join();
      })
    });
    console.log(a);
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 2020-07-25
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2020-10-08
      • 2017-12-24
      相关资源
      最近更新 更多