为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

眼眸繁星 2022-06-16 17:17:17
在给定的对象中,有不同的问题集。(标签,问题文本),每个对象集都有“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        }      ]    }  }使用上述解决方案,我得到了这个结果:请帮助我获得解决方案,或为每个跳过标签的问题文本以串行方式索引值。
查看完整描述

1 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

你应该循环li不上ul。它也可以以更简单的方式完成。


你应该这样做:


在component.ts文件中,按如下方式处理您的数据:


callGenerateConfig() {

    let data = [];

    data = this.globalJsonData.parent.child;

    let position = 1;


    data.forEach((item, index) => {

      let isLabel = item.QuestionTypeId === 18;

      console.log(isLabel);

      let obj = {

        label: isLabel,

        data: item,

        location: position

      }

      if (!isLabel) {

        position++;

      }

      this.questionList.push(obj);

    })

  }

在component.html下面做:


<ul>

   <li *ngFor="let data of questionList">

      <span *ngIf="data.label===false"> {{data.location}}</span>

      <span> {{data.data.QuestionText}}</span>

   </li>  

</ul>


查看完整回答
反对 回复 2022-06-16
  • 1 回答
  • 0 关注
  • 101 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号