So, I have two arrays, one called incart and the other called products
The incart array contains objects (representing an item they have in the cart). Each object is composed of a product id and a product qty.
Moreover, the products array contains all the products on the website.
For example, inside the products array there are objects of each item. It contains a bunch of info about the product. The important one here is the product price.
In short, I need to sum up all the prices. The complicated thing is matching the individual product qty with the product price which are found in two arrays.
So my set up is as follows:
The products array represents all the items that the user has in their cart, which is being set by filtering through the incart array, this is being done to obtain the price of each product.
const {incart} = useContext(ContextApp)
const [products, setProducts] = useState([])
useEffect(()=> {
db.collection('products').doc('products').onSnapshot(snap=> {
const productsdata = snap.data().products
setProducts(productsdata.filter(x=> incart.some(el=> x.id === el.id)))
})
},[incart])
Any idea how I can relate these two arrays, maybe to compress them into another array which would in short only contain the price of the item and the qty of the item.