gpt4 book ai didi

javascript - 如果两个数组具有相同的ownerId,如何比较它们

转载 作者:行者123 更新时间:2023-12-03 14:16:09 26 4
gpt4 key购买 nike

enter image description here如果两个数组具有相同的ownerId,我如何比较它们。第一个数组是 userUserInfo,第二个数组是orders 数组。我想比较它们是否具有相同的ownerId。2 个数组是从 firebase 获取的。如果ownerId ===到ownerId,我写的newu很难编码

这些是 console.log 中的数组

Array [
UserInfo {
"adress": "Flat 76",
"dateofbirth": "26-09-1995",
"email": “testt@yahoo.com",
"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>
);

};

最佳答案

结合使用过滤某些数组方法。

这是一个例子:

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

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


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

关于javascript - 如果两个数组具有相同的ownerId,如何比较它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60385933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com