猿问

如何在给定的对象中以串行方式检索索引值

在给定的对象中,有不同的问题集。(标签,问题文本),每个对象集都有“QuestionTypeId”。如果 QuestionTypeId 为“18”,则问题为“标签”,标签不应包含数字(索引值)。每个标签可能有一个或多个带有索引值的 questionText。

在这里,我只需要向 QuestionTypeId 不同而不是“18”的 questionText 显示索引值。只有那些问题文本编号或索引值需要连续添加,下面是以正确格式显示的图像。

图像中显示的结果是我试图得到


下面是我试图得到结果的代码:


export class AppComponent implements OnInit {

  globalJsonData = {

    "parent": {

      "child": [{

          "QuestionId": 349,

          "QuestionText": "This is Label One",

          "QuestionTypeId": 18,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 340,

          "QuestionText": "This is Question Text One",

          "QuestionTypeId": 17,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 350,

          "QuestionText": "This is Label Two",

          "QuestionTypeId": 18,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 352,

          "QuestionText": "This is Question Text Two",

          "QuestionTypeId": 17,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 354,

          "QuestionText": "This is Question Text Three",

          "QuestionTypeId": 6,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 350,

          "QuestionText": "This is Label Three",

          "QuestionTypeId": 18,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 353,

          "QuestionText": "This is Question Text Four",

          "QuestionTypeId": 17,

          "QuestionSetCode": 166378

        },

        {

          "QuestionId": 355,

          "QuestionText": "This is Question Text Five",

          "QuestionTypeId": 8,

          "QuestionSetCode": 166378

        }

      ]

    }

  }

使用上述解决方案,我得到了这个结果:

http://img3.mukewang.com/62aaf54700012b5805030373.jpg

请帮助我获得解决方案,或为每个跳过标签的问题文本以串行方式索引值。


眼眸繁星
浏览 103回答 1
1回答

素胚勾勒不出你

你应该循环li不上ul。它也可以以更简单的方式完成。你应该这样做:在component.ts文件中,按如下方式处理您的数据:callGenerateConfig() {&nbsp; &nbsp; let data = [];&nbsp; &nbsp; data = this.globalJsonData.parent.child;&nbsp; &nbsp; let position = 1;&nbsp; &nbsp; data.forEach((item, index) => {&nbsp; &nbsp; &nbsp; let isLabel = item.QuestionTypeId === 18;&nbsp; &nbsp; &nbsp; console.log(isLabel);&nbsp; &nbsp; &nbsp; let obj = {&nbsp; &nbsp; &nbsp; &nbsp; label: isLabel,&nbsp; &nbsp; &nbsp; &nbsp; data: item,&nbsp; &nbsp; &nbsp; &nbsp; location: position&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (!isLabel) {&nbsp; &nbsp; &nbsp; &nbsp; position++;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; this.questionList.push(obj);&nbsp; &nbsp; })&nbsp; }在component.html下面做:<ul>&nbsp; &nbsp;<li *ngFor="let data of questionList">&nbsp; &nbsp; &nbsp; <span *ngIf="data.label===false"> {{data.location}}</span>&nbsp; &nbsp; &nbsp; <span> {{data.data.QuestionText}}</span>&nbsp; &nbsp;</li>&nbsp;&nbsp;</ul>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答