[英]How to iterate Multiple states in a single table
This is simple table which have sr no, campaign id, campaign name, campaign status iterate from campaign state, and i have reach, sent,delivered etc on Stats state.这是一个简单的表格,其中包含从活动 state 迭代的 sr 号、活动 ID、活动名称、活动状态,并且我在统计信息 state 上已到达、发送、交付等。 How to iterate campaigns and stats in a single tr to get all the required data in one table?
如何在单个 tr 中迭代活动和统计数据以在一个表中获取所有必需的数据? campaigns and stats are two different states with data collected from different api.
活动和统计数据是两个不同的状态,数据来自不同的 api。
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Sr. No</th>
<th>Campaign Id</th>
<th>Campaign Name</th>
<th>Campaign Status</th>
<th>Reach</th>
<th>Sent</th>
<th>Delivered</th>
<th>Views</th>
<th>Clicks</th>
<th>Unsubscribes</th>
</tr>
</thead>
<tbody>
{campaigns.map((campaigns, index) => (
<tr>
<td>
{index + 1}
</td>
<td>
{campaigns.campaignId}
</td>
<td>
{campaigns.campaignTitle}
</td>
<td>
{campaigns.campaignStatus}
</td>
</tr>
))}
{stats.map((stats, index) => (
<tr>
<td>
{stats.data.reach}
</td>
<td>
{stats.data.sent}
</td>
<td>
{stats.data.delivered}
</td>
<td>
{stats.data.views}
</td>
<td>
{stats.data.clicks}
</td>
<td>
{stats.data.unsubscribed}
</td>
</tr>
))}
</tbody>
</table>
I assume that both array have same size, you can create oneother from this and use it我假设两个数组的大小相同,您可以从中创建一个并使用它
render() {
const joinedMap = [];
for(let x = 0; x < elem.length; x++){
joinedMap.push({
...campaigns[x],
...stats.data[x]
});
}
return (
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Sr. No</th>
<th>Campaign Id</th>
<th>Campaign Name</th>
<th>Campaign Status</th>
<th>Reach</th>
<th>Sent</th>
<th>Delivered</th>
<th>Views</th>
<th>Clicks</th>
<th>Unsubscribes</th>
</tr>
</thead>
<tbody>
{joinedMap.map((elem, index) => (
<tr>
<td>
{index + 1}
</td>
<td>
{elem.campaignId}
</td>
<td>
{elem.campaignTitle}
</td>
<td>
{elem.campaignStatus}
</td>
<td>
{elem.reach}
</td>
<td>
{elem.sent}
</td>
<td>
{elem.delivered}
</td>
<td>
{elem.views}
</td>
<td>
{elem.clicks}
</td>
<td>
{elem.unsubscribed}
</td>
</tr>
))}
</tbody>
</table>
);
}
Why not merge the data in advance?为什么不提前合并数据?
import React from "react";
const campaigns = [
{campaignId: "Id1", campaignTitle: "Title1", campaignStatus: "Status1"},
{campaignId: "Id2", campaignTitle: "Title2", campaignStatus: "Status2"},
{campaignId: "Id3", campaignTitle: "Title3", campaignStatus: "Status3"}
];
const stats = [
{data: {reach: "reach1", sent: "sent1", delivered: "delivered1"}},
{data: {reach: "reach2", sent: "sent2", delivered: "delivered2"}},
{data: {reach: "reach3", sent: "sent3", delivered: "delivered3"}}
];
const allData = campaigns.map((campaign, i) => ({
...campaign,
...stats[i].data
}));
export default function App() {
return (
<table className="table table-bordered table-hover">
<thead>
<tr>
<th>Sr. No</th>
<th>Campaign Id</th>
<th>Campaign Name</th>
<th>Campaign Status</th>
<th>Reach</th>
<th>Sent</th>
<th>Delivered</th>
</tr>
</thead>
<tbody>
{allData.map(
(
{
campaignId,
campaignTitle,
campaignStatus,
reach,
sent,
delivered
},
index
) => (
<tr key={index}>
<td>{index + 1}</td>
<td>{campaignId}</td>
<td>{campaignTitle}</td>
<td>{campaignStatus}</td>
<td>{reach}</td>
<td>{sent}</td>
<td>{delivered}</td>
</tr>
)
)}
</tbody>
</table>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.