-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (26 loc) · 1.23 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//merging a cart list object with their appropriate customer object without changing the original objects.
//Assuming you have a list of cart items and a list of customer objects
const cartList=[
{id:1, product:'Product 1', quantity:2},
{id:2, product:'Product 2', quantity:2},
{id:3, product:'Product 3', quantity:1}
]
const customerList=[
{id:1, name:'Customer 1', email:'[email protected]'},
{id:2, name:'Customer 2', email:'[email protected]'},
{id:3, name:'Customer 3', email:'[email protected]'}
]
//Function to merge cart item with appropriate customer
function mergeCartWithCustomer(cartItem,customerList){
const customer=customerList.find(customer=>customer.id===cartItem.id)
if(customer){
//Using spread operator to merge without modifying the original objects
return {...cartItem, customer};
// Alternatively, you can use Object.assign():
// return Object.assign({}, cartItem, { customer });
}
return cartItem; // Return the original cart item if no matching customer found
}
//Merging cart list with appropriate customer objects
const mergedCartList=cartList.map(cartItem=>mergeCartWithCustomer(cartItem,customerList))
console.log('mergedCartList',mergedCartList);