Javascript:循环遍历嵌套对象和数组

时间:2018-03-19 15:41:39

标签: javascript arrays object for-loop foreach

我有一个服务器响应,其数据具有结构,您可以在codesnippet中看到。 我的目标是遍历每个同意并添加频道

来自回复的数据:

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

enter image description here

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

2 个答案:

答案 0 :(得分:0)

您可以使用地图提取标题。并使用像这样的连接使它们联合起来。

       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'
                }
       ]

答案 1 :(得分:0)

我写了一段代码,希望这是你正在寻找的。它没有经过优化,请尽量优化

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