0

I have data:

   data = {
    "laptop": [
        "dell", 
        "hp", 
        "lenovo", 
        "acer", 
        "asus"
    ],
    "mobile": [
        "lenovo", 
        "motorolla", 
        "apple", 
        "samsung"
    ] 
}

I am trying to display it in the table using the ngFor for displaying the data in the below format

enter image description here

But I am unable to get the data in the below rather than I am getting only data in the traditional format leaving laptop,mobile keys is there any way to do that in the template

Stackblitz demo

is there any alternative approach or better becoz in future i may get n rows for the table

5
  • 1
    your stackblitz does not have any code Commented Aug 6, 2018 at 7:16
  • 1
    I can't see anything in your stackblitz. Also, it's not that easy to understand your question. What is "the traditional format"? Commented Aug 6, 2018 at 7:17
  • To get Object keys you can use Object.keys(data). The rest of this is straight forward. Commented Aug 6, 2018 at 7:20
  • @Chellappan actually i pasted it but it is not displaying it i dont know why Commented Aug 6, 2018 at 7:36
  • @Nick actually i pasted it but it is not displaying it i dont know why Commented Aug 6, 2018 at 7:36

2 Answers 2

2

If you are using angular 6.1 you can use keyvalue

<div *ngFor="let title of data | keyvalue">
  {{title.key}}
</div>

Example :https://stackblitz.com/edit/keyvalue-pipe

Sign up to request clarification or add additional context in comments.

Comments

1

You can use this :

get dataKeys() { return Object.keys(this.data); }

This will create an array of the keys of your object.

You can now use a loop on it :

<div *ngFor="let key of dataKeys">
  <div *ngFor="let item of data[key]">...</div>
</div>

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.