1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-11-09 22:08:58 +01:00
programming-challenges/challenges/inventory-update/solutions/javascript-inventory/solution.js
2020-07-08 17:17:03 +02:00

17 lines
484 B
JavaScript

function solution (array1, array2) {
const result = [...array1]
array2.forEach(delivery2 => {
const foundResultIndex = result.findIndex(
deliveryResult => deliveryResult.item === delivery2.item
)
if (foundResultIndex === -1) return result.push(delivery2)
return (result[foundResultIndex].quantity =
result[foundResultIndex].quantity + delivery2.quantity)
})
return result.sort((a, b) => a.item.localeCompare(b.item))
}
module.exports = solution