I am working on building a web application using reactJS. We should display the product users subscribed to. Number of products each user subscribes to differs. For Example, here is the response :
JavaScript
x
22
22
1
{
2
"data" : [
3
{
4
"user": "user1",
5
"subscriptions":
6
{
7
"user1_product_1" : 20,
8
"user1_product_2": 25
9
}
10
},
11
{
12
"user": "user2",
13
"subscriptions": {
14
"user2_product_1": 30,
15
"user2_product_2": 25,
16
"user2_product_3": 50,
17
"user2_product_4": 50
18
}
19
}
20
]
21
}
22
So, the subscription data is dynamic. How can I display the above data in tabular data as follow :Mock User can subscribe to any number of products.. as of now we don’t have users who subscribed to more than 4 products.
Advertisement
Answer
First of all , your data is a mess and it has not proper structure. correct it first and then this should might help you:
JavaScript
1
32
32
1
let data = [
2
{
3
user: "user1",
4
subscriptions: {
5
user1_product_1: 20,
6
user1_product_2: 25,
7
},
8
},
9
{
10
user: "user2",
11
subscriptions: {
12
user2_product_1: 30,
13
user2_product_2: 25,
14
user2_product_3: 50,
15
user2_product_4: 50,
16
},
17
},
18
];
19
20
const TabularData = (props) => (
21
<table>
22
{props.data.map((user) => (
23
<tr>
24
{Object.keys(user.subscriptions).map((user_product) => (
25
<td>{user.subscriptions[user_product]}</td>
26
))}
27
</tr>
28
))}
29
</table>
30
);
31
32