-
Notifications
You must be signed in to change notification settings - Fork 18
/
06.js
40 lines (33 loc) · 1.14 KB
/
06.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
37
38
39
40
// Add property to each object in array
// Write a function that takes an array of objects and a string as arguments
// Add a property with key 'continent' and value equal to the string to each of the objects
// Return the new array of objects
// Tipp: try not to mutate the original array
function myFunction(arr, str) {
arr.map((obj) => (obj.continent = str));
return arr;
// arr.forEach(el => el.continent = str);
// return arr;
// for (let key in arr) arr[key] = { ...arr[key], continent: str };
// return arr;
// AUTHOR'S:
// return arr.map((obj) => ({ ...obj, continent: str }));
}
console.log(
myFunction(
[
{ city: "Tokyo", country: "Japan" },
{ city: "Bangkok", country: "Thailand" },
],
"Asia"
)
); // [{ city: 'Tokyo', country: 'Japan', continent: 'Asia' }, { city: 'Bangkok', country: 'Thailand', continent: 'Asia' }]
console.log(
myFunction(
[
{ city: "Stockholm", country: "Sweden" },
{ city: "Paris", country: "France" },
],
"Europe"
)
); // [{ city: 'Stockholm', country: 'Sweden', continent: 'Europe' }, { city: 'Paris', country: 'France', continent: 'Europe' }]