-
Notifications
You must be signed in to change notification settings - Fork 18
/
07.js
36 lines (30 loc) · 1.15 KB
/
07.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
30
31
32
33
34
35
36
// Merge two objects with matching keys
// Write a function that takes two objects as arguments
// Unfortunately, the property 'country' in the second object has the wrong key
// It should be named 'city' instead
// Merge both objects and correct the wrong property name
// Return the resulting object
// It should have the properties 'planet', 'continent', 'country', 'state', and 'city'
function myFunction(a, b) {
// for (let key in b)
// if (key === "country") {
// b.city = b[key];
// delete b[key];
// }
// return { ...a, ...b };
// AUTHOR'S:
const { country, ...rest } = b;
return { ...a, ...rest, city: country };
}
console.log(
myFunction(
{ continent: "Europe", country: "Germany" },
{ planet: "Earth", country: "Munich", state: "Bavaria" }
)
); // { continent: 'Europe', country: 'Germany', planet: 'Earth', state: 'Bavaria', city: 'Munich'}
console.log(
myFunction(
{ continent: "North America", country: "USA" },
{ planet: "Earth", country: "Los Angeles", state: "California" }
)
); // { continent: 'North America', country: 'USA', planet: 'Earth', state: 'California', city: 'Los Angeles'}