S
S
Sir0zha2019-10-23 21:21:15
Swift
Sir0zha, 2019-10-23 21:21:15

How to merge two dictionaries?

Merge the two dictionaries into one called tables. If both dictionaries have the same key, sum the values. !!!Use only dictionaries and arrays
let table1 = ["box":3, "lamp":1, "pen":2]
let table2 = ["box":2, "PC":1, "pen": 3]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doublench21, 2019-10-24
@Sir0zha

5db18233eab0c251752676.png

text
let table1 = ["box": 3, "lamp": 1, "pen": 2]
let table2 = ["box": 2, "PC": 1, "pen": 3]

let table = table1.merging(table2) { $0 + $1 }

// ["pen": 5, "lamp": 1, "PC": 1, "box": 5]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question