How to Loop Object keys and display in table using angular 2+

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to Loop Object keys and display in table using angular 2+



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


ngFor



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





your stackblitz does not have any code
– Chellappan
Aug 6 at 7:16





I can't see anything in your stackblitz. Also, it's not that easy to understand your question. What is "the traditional format"?
– Nick
Aug 6 at 7:17





To get Object keys you can use Object.keys(data). The rest of this is straight forward.
– sabithpocker
Aug 6 at 7:20


Object.keys(data)





@Chellappan actually i pasted it but it is not displaying it i dont know why
– 2 Developer
Aug 6 at 7:36





@Nick actually i pasted it but it is not displaying it i dont know why
– 2 Developer
Aug 6 at 7:36




2 Answers
2



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>



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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard