-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDay3P1.js
37 lines (26 loc) · 836 Bytes
/
Day3P1.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
/*
Convert an Array of Objects to an Array of Strings Using map().
Task: You have an array of user objects, and you need to create an array of their names.
//output
['Alice', 'Bob', 'Charlie', 'David', 'Eve', 'Frank', 'Grace', 'Hank', 'Ivy']
*/
const users = [
{ name: 'Alice', age: 21 },
{ name: 'Bob', age: 22 },
{ name: 'Charlie', age: 21 },
{ name: 'David', age: 22 },
{ name: 'Eve', age: 23 },
{ name: 'Frank', age: 21 },
{ name: 'Grace', age: 23 },
{ name: 'Hank', age: 24 },
{ name: 'Ivy', age: 22 }
];
// Implement your logic here and store the result as "userNames".
let usernames = [];
users.forEach(obj => userNames.push(obj.name));
console.log(userNames);
/*
another way of doing
let userNames = users.map(user => user.name);
console.log(userNames); // Correct output
*/