-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-to-objects.js
More file actions
45 lines (34 loc) · 1.26 KB
/
map-to-objects.js
File metadata and controls
45 lines (34 loc) · 1.26 KB
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
41
42
43
44
45
// You have an array of user objects, each one has name, surname and id.
// Write the code to create another array from it, of objects with id and fullName, where fullName is generated from name and surname.
// For instance:
// let john = { name: "John", surname: "Smith", id: 1 };
// let pete = { name: "Pete", surname: "Hunt", id: 2 };
// let mary = { name: "Mary", surname: "Key", id: 3 };
// let users = [ john, pete, mary ];
// let usersMapped = /* ... your code ... */
// /*
// usersMapped = [
// { fullName: "John Smith", id: 1 },
// { fullName: "Pete Hunt", id: 2 },
// { fullName: "Mary Key", id: 3 }
// ]
// */
// alert( usersMapped[0].id ) // 1
// alert( usersMapped[0].fullName ) // John Smith
// So, actually you need to map one array of objects to another. Try using => here. There’s a small catch.
let john = { name: "John", surname: "Smith", id: 1 };
let pete = { name: "Pete", surname: "Hunt", id: 2 };
let mary = { name: "Mary", surname: "Key", id: 3 };
let users = [ john, pete, mary ];
let usersMapped = users.map(user => {
let fullName = user.name + " " + user.surname;
return {
fullName,
id: user.id,
}
});
console.log(usersMapped);
console.log(usersMapped[0].id);
// 1
console.log(usersMapped[0].fullName);
// John Smith