mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-11-09 22:08:58 +01:00
17 lines
484 B
JavaScript
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
|