how to compare two arrays together if they have the same ownerId

jana :

enter image description herehow can i compare 2 arrays together if they have the same ownerId. the fist array is the userUserInfo the second one is the orders array. I want to compare if thet have the same ownerId. the 2 array are fetching from firebase. the newu I wrote is hardly coded if the ownerId is === to the ownerId

these are the arrays i console.log

Array [
  UserInfo {
    "adress": "Flat 76",
    "dateofbirth": "26-09-1995",
    "email": “[email protected]",
    "id": "-M0s8z5kilepQfGaDarw",
    "namee": “testt”,
    "ownerId": "XS7ARDt5C8hKtQyTSrPh9oLKMUp2",
    "phonenumber": "079112",
  },
 ]
Array [
  Order {
    "date": 2020-02-24T20:46:24.981Z,
    "id": "-M0swr3CHlIJczaLxppg",
    "items": Array [
      Object {
        "productId": "-M0swATocaxb_VboxWrd",
        "productPrice": 10,
        "productTitle": "Water",
        "quantity": 1,
        "sum": 10,
      },
    ],
    "ownerId": "XS7ARDt5C8hKtQyTSrPh9oLKMUp2",
    "productTitle": undefined,
    "totalAmount": 10,
  },
]

const OrderItem = props => {
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState();

  const [showDetails, setShowDetails] = useState(false);
  const dispatch = useDispatch();
  const orders = useSelector(state => state.orders.orders);

  const userUserInfo = useSelector(state => state.userInfo.userUserInfo)

//const newu = userUserInfo.filter(prod=>prod.ownerId === 'XS7ARDt5C8hKtQyTSrPh9oLKMUp2')



 console.log(userUserInfo)
 console.log(orders)


  return (
    <Card style={styles.orderItem}>
      <View style={styles.summary}>
        <Text style={styles.totalAmount}>£{props.amount}</Text>
        {/* <Text style={styles.totalAmount}>{props.date}</Text> */}
        <Text style={styles.totalAmount}>t{props.items.title}</Text>


      </View>
      <Button
        color={Colors.primary}
        title={showDetails ? 'Hide Details' : 'Show Details'}
        onPress={() => {
          setShowDetails(prevState => !prevState);
        }}
      />
      {showDetails && (
        <View style={styles.detailItems}>
          {props.items.map(cartItem => (
            <CartItem
              key={cartItem.productId}
              quantity={cartItem.quantity}
              amount={cartItem.sum}
              title={cartItem.productTitle}


            />

          ))}
          <FlatList
          data={userUserInfo}
          keyExtractor={item => item.id}
          renderItem={itemData => (
            <Profile

            image={itemData.item.imageUrl}
                    title={itemData.item.namee}
                    price={itemData.item.email}
                    phonenumber={itemData.item.phonenumber}
                    adress={itemData.item.adress}
                    quantityNeeded={itemData.item.dateofbirth}
                    onSelect={() => {
                        selectItemHandler(itemData.item.id, itemData.item.namee);
                    }} >

          </Profile>
           )} />


        </View>
      )}
    </Card>    
  );

};
Eugen Sunic :

Use a combination of filter and some array methods.

Here is an example:

const userInfo = [{
  "ownerId": "XS7ARDt5C8hKtQyTSrPh9oLKMUp2"
}]

const order = [{
  "ownerId": "XS7ARDt5C8hKtQyTSrPh9oLKMUp2",
}]


const res = userInfo.filter(({
  ownerId
}) => order.some(x => x.ownerId === ownerId));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=12039&siteId=1