Last active
July 14, 2020 07:25
-
-
Save NickSolante/7a84a80ece4ea530174587425f5c3539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const data = [ | |
{ | |
id: 1, | |
order_id: 1, | |
price_per_unit: 1.3454, | |
quantity: 10, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 2, | |
order_id: 2, | |
price_per_unit: 23.14, | |
quantity: 11, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 3, | |
order_id: 3, | |
price_per_unit: 123.0345, | |
quantity: 12, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 4, | |
order_id: 4, | |
price_per_unit: null, | |
quantity: 13, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 5, | |
order_id: 5, | |
price_per_unit: 100, | |
quantity: 14, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 6, | |
order_id: 6, | |
price_per_unit: 1.5454, | |
quantity: 15, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 7, | |
order_id: 7, | |
price_per_unit: 25.14, | |
quantity: 16, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 8, | |
order_id: 8, | |
price_per_unit: 133.0345, | |
quantity: 17, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 9, | |
order_id: 9, | |
price_per_unit: 13.456, | |
quantity: 18, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 10, | |
order_id: 10, | |
price_per_unit: 110, | |
quantity: 19, | |
product: 'Corrugated Box', | |
}, | |
{ | |
id: 11, | |
order_id: 1, | |
price_per_unit: 45.2334, | |
quantity: 20, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 12, | |
order_id: 2, | |
price_per_unit: null, | |
quantity: 21, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 13, | |
order_id: 3, | |
price_per_unit: 273.1234, | |
quantity: 22, | |
product: 'Hand sanitiZER', | |
}, | |
{ | |
id: 14, | |
order_id: 4, | |
price_per_unit: 11.45, | |
quantity: 23, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 15, | |
order_id: 5, | |
price_per_unit: 12.467, | |
quantity: 24, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 16, | |
order_id: 6, | |
price_per_unit: 11, | |
quantity: 25, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 17, | |
order_id: 7, | |
price_per_unit: 123, | |
quantity: 26, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 18, | |
order_id: 8, | |
price_per_unit: 173.1234, | |
quantity: 27, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 19, | |
order_id: 9, | |
price_per_unit: 23.876, | |
quantity: 28, | |
product: 'Hand sanitizer', | |
}, | |
{ | |
id: 20, | |
order_id: 10, | |
price_per_unit: 120, | |
quantity: 29, | |
product: 'Hand sanitizer', | |
}, | |
] | |
const data2 = [ | |
{ | |
id: 1, | |
delivery_identifier: 1, | |
order_item_id: 1, | |
delivered_quantity: 5, | |
}, | |
{ | |
id: 2, | |
delivery_identifier: 2, | |
order_item_id: 2, | |
delivered_quantity: 11, | |
}, | |
{ | |
id: 3, | |
delivery_identifier: 3, | |
order_item_id: 3, | |
delivered_quantity: 12, | |
}, | |
{ | |
id: 4, | |
delivery_identifier: 4, | |
order_item_id: 4, | |
delivered_quantity: 3, | |
}, | |
{ | |
id: 5, | |
delivery_identifier: 5, | |
order_item_id: 6, | |
delivered_quantity: 15, | |
}, | |
{ | |
id: 6, | |
delivery_identifier: 6, | |
order_item_id: 7, | |
delivered_quantity: 8, | |
}, | |
{ | |
id: 7, | |
delivery_identifier: 7, | |
order_item_id: 8, | |
delivered_quantity: 3, | |
}, | |
{ | |
id: 8, | |
delivery_identifier: 8, | |
order_item_id: 16, | |
delivered_quantity: 25, | |
}, | |
{ | |
id: 9, | |
delivery_identifier: 9, | |
order_item_id: 17, | |
delivered_quantity: 26, | |
}, | |
{ | |
id: 10, | |
delivery_identifier: 10, | |
order_item_id: 18, | |
delivered_quantity: 27, | |
}, | |
{ | |
id: 11, | |
delivery_identifier: 11, | |
order_item_id: 19, | |
delivered_quantity: 28, | |
}, | |
{ | |
id: 12, | |
delivery_identifier: 12, | |
order_item_id: 20, | |
delivered_quantity: 29, | |
}, | |
{ | |
id: 13, | |
delivery_identifier: 13, | |
order_item_id: 4, | |
delivered_quantity: 5, | |
}, | |
{ | |
id: 14, | |
delivery_identifier: 14, | |
order_item_id: 8, | |
delivered_quantity: 8, | |
}, | |
{ | |
id: 15, | |
delivery_identifier: 15, | |
order_item_id: 8, | |
delivered_quantity: 6, | |
}, | |
] | |
const mergeDeliveryAndOrderItem = (orderItem, delivery) => | |
orderItem.map((itm) => ({ | |
...delivery.find((item) => item.order_item_id === itm.id && item), | |
...itm, | |
})) | |
const mng = mergeDeliveryAndOrderItem(data, data2) | |
const accumulator = mng.map((data) => ({ | |
id: data.id, | |
order_item_id: data.order_item_id, | |
delivered_quantity: data.delivered_quantity, | |
order_id: data.order_id, | |
price_per_unit: data.price_per_unit, | |
quantity: data.quantity, | |
total: | |
typeof data.price_per_unit === null | |
? '-' | |
: data.quantity * data.price_per_unit, | |
product: data.product, | |
})) | |
console.log(mng) | |
console.log('===========================================================') | |
console.log(accumulator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment