简体   繁体   English

使用子数组值读取 json 并使用 ngFor 在 html 上显示

[英]Read the json with sub array values and display on the html using ngFor

Hi I am developing a web app where I am display a student data received in json format.嗨,我正在开发一个 web 应用程序,我在其中显示以 json 格式接收的学生数据。

Here is the typescript code snippet这是 typescript 代码片段

    export interface studentData{
      ID : number;
      Name :string;
      Class : string;
      Languages: string;
    }
    
    const STUDENT_DATA: studentData[] = 
    [
     {
      ID : 1
      Name: "Amy",
      Class: "Grade1",
      Languages: "Java, .net, Python"
     },
     {
      ID : 2
      Name: "John",
      Class: "Grade2",
      Languages: "Kotlin, Java, Typescript"
     },
     {
      ID: 3
      Name: "Shawn",
      Class: "Grade3",
      Languages: "Javascript, C++, Perl"
     },
    ]
export class StudentDataComponent{
       languages : string[] = [];
    for (let i=0; i <= STUDENT_DATA.length - 1 ; i++)
     {
       this.languages = STUDENT_DATA[i].Languages.split(",");
     }
}

I tried to make Languages as separate array and thought to use it while displaying on screen using ngFor我尝试将 Languages 作为单独的数组,并考虑在使用 ngFor 在屏幕上显示时使用它

languages : string[] = [];
for (let i=0; i <= STUDENT_DATA.length - 1 ; i++)
 {
   this.languages = STUDENT_DATA[i].Languages.split(",");
 }

I tried to display in mat-chip-list as shown below but it just displays ID 3 languages for all ID's我尝试在 mat-chip-list 中显示,如下所示,但它只显示所有 ID 的 ID 3 语言

<mat-chip-list>
   <mat-chip *ngFor = "let lang of languages>
         {{lang}}
   </mat-chip>
</mat-chip-list>

Need help to read the languages json value and display over screen.需要帮助来阅读语言 json 值并在屏幕上显示。

I guess you would like to merge all languages into one array.我猜您想将所有语言合并到一个数组中。 You can achieve it like this:你可以像这样实现它:

this.languages = [...this.languages, ...STUDENT_DATA[i].Languages.split(",")];

"it just displays ID 3 languages for all ID's" “它只显示所有 ID 的 ID 3 语言”

this is because you are setting this.language to the first, then the second... at the end it just ends up being the last item.这是因为您将this.language设置为第一个,然后是第二个......最后它只是最后一个项目。 You don't have a separate one for each student.您没有为每个学生单独设置一个。

I think a better alternative would be to add a property on each student object.我认为更好的选择是在每个学生 object 上添加一个属性。 Something like:就像是:

STUDENT_DATA.map(s => { ...s, LanguageArray: s.Language.split(",") })

If you're just wanting to loop through the languages string for each student in the loop, you can do your split() inline with a second ngFor, as in如果您只想循环遍历循环中每个学生的语言字符串,您可以将split()与第二个 ngFor 内联,如

<div *ngFor="let student of STUDENT_DATA">
 <!-- .... -->
  <mat-chip-list>
     <mat-chip *ngFor="let lang of student.languages.split(',')">
         {{lang}}
     </mat-chip>
  </mat-chip-list>
</div>

Everything's already been suggested, I'd just like to point out that you need to do split(', ') - with a space after comma - or your arrays will have strings like ' Java', '.一切都已经被建议了,我只想指出你需要做 split(', ') - 逗号后有一个空格 - 否则你的 arrays 将有像'Java','这样的字符串。 net', ' Python' as well as 'Java', 'Kotlin' etc. If at some point you want to list all the languages (from all students), you don't want to have both 'Java' and ' Java' in it. net'、'Python' 以及 'Java'、'Kotlin' 等。如果在某个时候你想列出所有语言(来自所有学生),你不想同时拥有 'Java' 和 'Java'在里面。 Also, I guess it's just a typo, but ID key's value in object should end with comma.另外,我想这只是一个错字,但 object 中的 ID 键值应该以逗号结尾。

Your code currently overwrites the language variable during every iteration.您的代码当前在每次迭代期间都会覆盖语言变量。 Modify the type of languages and add index also to languages variable:修改语言类型并将索引也添加到语言变量:

let languages: string[][] = [];

for (let i = 0; i <= STUDENT_DATA.length - 1; i++) {
    languages[i] = STUDENT_DATA[i].Languages.split(",");
}

Code should be something like that:代码应该是这样的:

<student-element *ngFor="let student of STUDENT_DATA; index as i">
// Element code here
<mat-chip-list>
  <mat-chip *ngFor = "let lang of languages[i]">
    {{lang}}
  </mat-chip>
</mat-chip-list>
</student-element>

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

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