0

Basically, I have this data

{
   [
      {
         "name":"aiman",
         "subject":[
            {
               "name_subject":"Malay_language",
               "code":123
            },
            {
               "name_subject":"English_language",
               "code":1234
            }
         ]
      },
      {
         "name":"wani",
         "subject":[
            {
               "name_subject":"Malay_language",
               "code":123
            }
         ]
      }
   ]
}

Basically what should I do in order getting this?

student name number of subject
Aiman 2
Wani 1

I have tried this, but it does not work..

 <tr *ngFor="let student of students">
                
                <td>{{student.name}}</td>
                <td>{{student.subject.length}}</td>
               
              
</tr>

1 Answer 1

1

You can do this in a variance of ways through the html you can add an index and a counter

<tr *ngFor="let student of students; let i=index">
    <td *ngIf="student.subject===math">{{I+1}}</td>
    <td>{{ student.name }}</td>
    <td>{{ student.subject.length }}</td>
</tr>

or in the component you can use forEach and count it

studentMath?: number;
studentDAta.forEach((student)=>{
if(student.class===math){
this.studentMath= ++;
})

or you reduce the value of the array and count it

studentData
        .reduce(function(prev, curr){
            return (curr.studentMath) ? prev.concat(curr.studentMath) : prev;
        }, [])
        .map(function(q){ 
            return (q.value)? q.value : 0;
        })
        .reduce(function(prev, curr){
            return prev + curr;
        }, 0);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.