Combine two arrays after using filter method
Use map and object spread to combine the two arrays:
var newArray = cartProducts.map(cart => ({
...cart,
...productDetails.find(prod => prod.productID === cart.productID)
}));
You can use map() after filter()
and add quantity
property to each item.
const productDetails = [{ productID: 1, productTitle: 'Product Title 1', productPrice: 2000 }, { productID: 2, productTitle: 'Product Title 2', productPrice: 5000 }, { productID: 3, productTitle: 'Product Title 3', productPrice: 1000 }, { productID: 4, productTitle: 'Product Title 4', productPrice: 10000 }];
const cartProducts = [{ productID: 1, quantity: 5 }, { productID: 3, quantity: 2 }];
function cartItemDetails() {
return productDetails
.filter(el => cartProducts.some(f => f.productID === el.productID))
.map(item => ({
...item,
"quantity": cartProducts.find(f => f.productID === item.productID).quantity
}));
}
console.log(cartItemDetails());
Or you can use reduce().
const productDetails = [{ productID: 1, productTitle: 'Product Title 1', productPrice: 2000 }, { productID: 2, productTitle: 'Product Title 2', productPrice: 5000 }, { productID: 3, productTitle: 'Product Title 3', productPrice: 1000 }, { productID: 4, productTitle: 'Product Title 4', productPrice: 10000 }];
const cartProducts = [{ productID: 1, quantity: 5 }, { productID: 3, quantity: 2 }];
function cartItemDetails() {
return productDetails.reduce((acc, curr) => {
let item = cartProducts.find(f => f.productID === curr.productID);
if (item) {
acc.push({ ...curr,
"quantity": item.quantity
});
}
return acc;
}, []);
}
console.log(cartItemDetails());
You can also use map()
on cartProducts
.
const productDetails = [{ productID: 1, productTitle: 'Product Title 1', productPrice: 2000 }, { productID: 2, productTitle: 'Product Title 2', productPrice: 5000 }, { productID: 3, productTitle: 'Product Title 3', productPrice: 1000 }, { productID: 4, productTitle: 'Product Title 4', productPrice: 10000 }];
const cartProducts = [{ productID: 1, quantity: 5 }, { productID: 3, quantity: 2 }];
function cartItemDetails() {
return cartProducts.map(item => ({
...productDetails.find(f => f.productID === item.productID),
...item
}));
}
console.log(cartItemDetails());
Convert the productDetails
to a Map (productDetailsMap
), using the productID
as the key.
Iterate the cartProducts
with Array.map()
get the current product from productDetailsMap
by it's productID
, and merge by spreading into a new object.
const productDetails = [{"productID":1,"productTitle":"Product Title 1","productPrice":2000},{"productID":2,"productTitle":"Product Title 2","productPrice":5000},{"productID":3,"productTitle":"Product Title 3","productPrice":1000},{"productID":4,"productTitle":"Product Title 4","productPrice":10000}]
const cartProducts = [{"productID":1,"quantity":5},{"productID":3,"quantity":2}]
const productDetailsMap = new Map(productDetails.map(o => [o.productID, o]))
const result = cartProducts.map(o => ({
...productDetailsMap.get(o.productID),
...o
}))
console.log(result)