简体   繁体   English

Javascript:遍历嵌套对象和数组

[英]Javascript: Loop through nested objects and arrays

I have an server response with data which has the structur as you can see in the codesnippet. 我有一个服务器响应,其中包含具有代码片段中所示结构的数据。 My goal is to iterate through each consent and add the channels 我的目标是遍历每个同意并添加渠道

The data from the response: 来自响应的数据:

[
  {
      "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"
      },
    ]

The way i go is the following. 我的方法如下。 But i need a way to append the channels to each Consent. 但是我需要一种将渠道附加到每个同意书的方法。 Is there a way to solve this? 有办法解决吗?

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
          }]
        });
      }
    });

在此处输入图片说明

I want to output the individual channels where channels is on the top of each entry 我想输出单个通道,其中通道位于每个条目的顶部

You can extract the titles using map. 您可以使用地图提取标题。 And concantanate them using join like this. 并像这样使用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'
                }
       ]

I have written a piece of code, hope this is what you're looking for. 我已经编写了一段代码,希望这是您想要的。 It is not optimized, try to optimize at your will 它尚未优化,请随意进行优化

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM