Angular2 Getting very deep nested json value using pipe! *ngFor

Lea

Hi I am having trouble getting json value which is really deeply nested using pipe.

What am I doing wrong?

Pipe I'm using

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'keyValues'
})
export class KeysPipe implements PipeTransform {
  transform(value, args: string[]): any {
    let keys = [];
    for (let key in value) {
      keys.push({
        key: key,
        value: value[key]
      });
    }
    return keys;
  }
}

Json I'm getting from server.

data:
  0: {
    Profile: { ...
    }
    BasicInfo: { ...
    }
    introduceInfo: {
      curriculum: { ...
      }
      experience: {
        0: {
          category: "Mentor"
          year: "2011"
          duration: "24"
        }
        1: {
          category: "Student"
          year: "2011"
          duration: "14"
        }
      }
    }
  }

It's actually a huge json object but I've simplified to only show what I need to get.

I want to get the value of category (which is "Mentor"and "Student".

And to do so, I've tried in my html

<div *ngFor="let detail of teaInfo | keyValues">
  <div *ngFor="let experience of detail.value['introduceInfo'] | keyValues">
    <div *ngFor="let exp of experience.value['experience'] | keyValues">
      <p class="fontstyle2">{{exp.value['category']}} {{exp.value['year']}}년 | {{ex.value['duration']}}개월</p>
    </div>
  </div>
</div>

And I'm getting my json object in my component like this.

teaInfo: any[];
getTeacherDetail(): void {
  let params = new URLSearchParams();
  params.set('gradeType', `${this.getVal2()}`)
  params.set('subjectType', `${this.getVal3()}`)
  params.set('district', `${this.getVal1()}`)

  this.teaDetail.getTeachersDetail(params)
    .subscribe(
      teaInfo => this.teaInfo = teaInfo,
      error => this.errorMessage = error
    )
}

And the result is I am getting nothing

What am I doing wrong?

AJT82

Trying to interpret how your JSON looks like, something like this:

{
  "data":{
    "0": {
      "Profile":{
        "prof":"prof"
      },
      "BasicInfo":{
        "basic":"basic"
      },
      "introduceInfo":{
        "curriculum": {
          "curr":"curr"
        },
        "experience":{
          "0":{
            "category":"Mentor",
            "year":"2011",
            "duration":"24"
          },
          "1":{
            "category":"Student",
            "year":"2011",
            "duration":"14"            
          }
        }
      }
    }
  }
}

In below example, I have extracted the values from data, so:

.map(res => res.json().data)

To reach values Mentor and Student, first change your pipe to this:

export class KeysPipe implements PipeTransform {
  transform(value: any, args: any[] = null): any {
    return Object.keys(value).map(key => value[key]);
  }
}

and change your HTML to this:

<div *ngFor="let detail of teaInfo | keyValues">
 <div *ngFor="let experience of detail['introduceInfo']['experience'] | keyValues">
    {{experience.category}}
 </div>
</div>

This should work nicely:

Demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Deep nested json to array in array / json rendering of Angular 2 *ngFor

From Dev

Getting a specific value from an angular2 *ngFor array

From Dev

Angular 2 using ngFor with the async pipe and index

From Dev

How to parse json file in angular2 using multiple ngFor?

From Dev

How to get selected value of nested object array using ngFor in Angular?

From Dev

Getting the value from a select using nested model in angular 2

From Dev

Angular 2: Nested *ngFor JSON data

From Dev

Angular 2 getting json data with ngfor

From Dev

Get outer json key value into very json in the nested json array using JOLT

From Dev

Getting key and value from JSON with angular2

From Dev

Getting key and value from JSON with angular2

From Dev

angular2 *ngfor and nested observable

From Dev

Using async pipe with ngFor

From Java

Using an array from Observable Object with ngFor and Async Pipe Angular 2

From Java

NgFor doesn't update data with Pipe in Angular2

From Dev

Angular2 - passing ngFor item into a pipe as a parameter?

From Dev

Angular2 ngFor - skip if no value

From Dev

Angular2 pipe dynamic value not taking

From Dev

Getting json value from nested response

From Dev

Override as_json to include specific value in deep nested model

From Dev

Override as_json to include specific value in deep nested model

From Dev

Nested ngFor with two-way data binding Angular2

From Dev

Getting 2 different decrypted values from very similar AES implementation using cryptoJS (getting a wrong result value for the 16th char)

From Dev

Getting values from nested JSON using AngularJS

From Dev

Angular 2 nested ngFor not iterating

From Dev

angular2 getting value of custom attribute

From Dev

Angular2 access nested JSON

From Dev

How to pipe JSON output of curl to query a JSON value using python

From Dev

How to cache a value in an ngFor directive in Angular2?

Related Related

  1. 1

    Deep nested json to array in array / json rendering of Angular 2 *ngFor

  2. 2

    Getting a specific value from an angular2 *ngFor array

  3. 3

    Angular 2 using ngFor with the async pipe and index

  4. 4

    How to parse json file in angular2 using multiple ngFor?

  5. 5

    How to get selected value of nested object array using ngFor in Angular?

  6. 6

    Getting the value from a select using nested model in angular 2

  7. 7

    Angular 2: Nested *ngFor JSON data

  8. 8

    Angular 2 getting json data with ngfor

  9. 9

    Get outer json key value into very json in the nested json array using JOLT

  10. 10

    Getting key and value from JSON with angular2

  11. 11

    Getting key and value from JSON with angular2

  12. 12

    angular2 *ngfor and nested observable

  13. 13

    Using async pipe with ngFor

  14. 14

    Using an array from Observable Object with ngFor and Async Pipe Angular 2

  15. 15

    NgFor doesn't update data with Pipe in Angular2

  16. 16

    Angular2 - passing ngFor item into a pipe as a parameter?

  17. 17

    Angular2 ngFor - skip if no value

  18. 18

    Angular2 pipe dynamic value not taking

  19. 19

    Getting json value from nested response

  20. 20

    Override as_json to include specific value in deep nested model

  21. 21

    Override as_json to include specific value in deep nested model

  22. 22

    Nested ngFor with two-way data binding Angular2

  23. 23

    Getting 2 different decrypted values from very similar AES implementation using cryptoJS (getting a wrong result value for the 16th char)

  24. 24

    Getting values from nested JSON using AngularJS

  25. 25

    Angular 2 nested ngFor not iterating

  26. 26

    angular2 getting value of custom attribute

  27. 27

    Angular2 access nested JSON

  28. 28

    How to pipe JSON output of curl to query a JSON value using python

  29. 29

    How to cache a value in an ngFor directive in Angular2?

HotTag

Archive