2

i´m new to Angular 2 in Typescript.I want to access D and G of my JSON with NgFor. Is there a way to access the elements?

[
  {
   "A":"B",
   "C":{
      "D": ["E","F"],
      "G": ["H"]
 } 
}

]

I also createt a Plunker: Plunker

1
  • Is there a problem with simply... accessing them? it's just your avg every day object. Commented Jun 27, 2016 at 18:49

1 Answer 1

3

ngFor can't iterate over an object's keys out of the box. You must handle that yourself.

Pipes work well. Example: Updated Plunkr

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
    transform(value: any, args?: any[]): any[] {
        let keys = Object.keys(value),
            data = [];

        keys.forEach(key => {
            data.push(value[key]);
        });

        return data;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Okay. Thank you. You cant read anything about it in the documentation.
I find that the documentation on the angular angular.io to be fantastic. Aside from reading articles that's the go to source for me.

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.