[英]Comparing two Arrays.map values dynamically
I have two arrays first array Deals
contains deal list where it has dealId
, buyerId
, sellerId
and, the second array Customer
contains customers list where it has customerId
, name
. 我有两个数组第一个数组
Deals
包含其中有交易清单dealId
, buyerId
, sellerId
,而在第二阵列Customer
包含一个客户列表,其中有customerId
, name
。 I just want to compare Deal.sellerId
to Customer.customerId
and want to show the Customer.name
. 我只想将
Deal.sellerId
与Customer.customerId
进行比较,并想要显示Customer.name
。
Don't confuse with the <DealDetail />
its a component which has attribute sellerName
, for this component I want customer name value. 不要将其与
<DealDetail />
混淆,它是一个具有sellerName
属性的sellerName
,对于这个组件,我需要客户名称值。
Deals Array: 交易数组:
this.state.updatedDeals.map(deal => <DealDetail key={deal.id} dealID={deal.id} sellerName={deal.sellerId} buyerName={deal.buyerId} />)
Customers Array: 客户数组:
this.state.updatedCustomers.map(customer => <li key={customer.id} > {customer.name} </li>)>
What I exactly wanting: 我到底想要什么:
<DealDetail sellerName={deal.sellerId===customer.customerId ? customer.name} : "Unknown" />)
You can try something like below. 您可以尝试以下类似方法。
this.state.updatedDeals.map(
(deal) =>
{
const c = this.state.updatedCustomers.filter(customer=>deal.sellerId===customer.id);
<DealDetail
key={deal.id}
dealID={deal.id}
sellerName={c.length ==1 ? c[0].name:'unknown'}
buyerName={deal.buyerId}
/>
}
)
let updatedDeals = [
{dealId: 10, buyerId: 1, sellerId: 26},
{dealId: 11, buyerId: 1, sellerId: 26},
{dealId: 12, buyerId: 1, sellerId: 27},
];
let updatedCustomers = [
{customerId: 26, customerName: 'Isaac'},
{customerId: 28, customerName: 'Jane'}
];
let DealDisplay = [];
updatedCustomers.forEach(cust => {
if(updatedDeals.some(deal => deal.sellerId === cust.customerId)){
DealDisplay.push(cust);
}
});
Why not separate the logic in order to promote maintainability and readability. 为什么不分离逻辑以提高可维护性和可读性。 You can extract the records into a separate array as temporary variable.
您可以将记录作为临时变量提取到单独的数组中。 Then render the view using the variable
然后使用变量渲染视图
Fetched data from firestore: 从Firestore获取的数据:
updatedCustomers[doc.id] = doc.data();
And solved above problem with this: 并解决了以上问题:
this.state.updatedDeals.map(deal => (<DealDetail key={deal.id}
sellerName={this.state.updatedCustomers[deal.sellerId].name}
buyerName={this.state.updatedCustomers[deal.buyerId].name}
/>))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.