Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态比较两个array.map值_Javascript_Arrays_Node.js_Reactjs - Fatal编程技术网

Javascript 动态比较两个array.map值

Javascript 动态比较两个array.map值,javascript,arrays,node.js,reactjs,Javascript,Arrays,Node.js,Reactjs,我有两个数组第一个数组Deals包含交易列表,其中有dealId,buyerId,sellerId,第二个数组Customer包含客户列表,其中有customerId,name。我只想将Deal.sellerId与Customer.customerId进行比较,并显示Customer.name 不要与混淆,它是一个具有属性sellerName的组件,对于这个组件,我需要客户名称值 交易阵列: this.state.updatedeals.map(交易=>) 客户阵列: this.state.up

我有两个数组第一个数组
Deals
包含交易列表,其中有
dealId
buyerId
sellerId
,第二个数组
Customer
包含客户列表,其中有
customerId
name
。我只想将
Deal.sellerId
Customer.customerId
进行比较,并显示
Customer.name

不要与
混淆,它是一个具有属性
sellerName
的组件,对于这个组件,我需要客户名称值

交易阵列:

this.state.updatedeals.map(交易=>)

客户阵列:

this.state.updatedCustomers.map(customer=>
  • {customer.name}
  • )>

    我真正想要的是:


    您可以尝试以下方法

    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} 
            />
        }
    )
    
    this.state.updatedeals.map(
    (交易)=>
    { 
    const c=this.state.updatedCustomers.filter(customer=>deal.sellerId==customer.id);
    }
    )
    

    为什么不分离逻辑以提高可维护性和可读性。您可以将记录作为临时变量提取到单独的数组中。然后使用变量

    从firestore获取的数据渲染视图:

    updatedCustomers[doc.id] = doc.data();
    
    并以此解决了上述问题:

    this.state.updatedDeals.map(deal => (<DealDetail key={deal.id}
    
             sellerName={this.state.updatedCustomers[deal.sellerId].name}
             buyerName={this.state.updatedCustomers[deal.buyerId].name}
    
           />))
    
    this.state.updatedeals.map(交易=>())
    
    do
    updatedDeals
    updatedCustomers
    始终具有一对一映射,或者它们可以具有不同的长度
    this.state.updatedDeals.map(deal => (<DealDetail key={deal.id}
    
             sellerName={this.state.updatedCustomers[deal.sellerId].name}
             buyerName={this.state.updatedCustomers[deal.buyerId].name}
    
           />))