-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_data.js
161 lines (149 loc) · 3.52 KB
/
transform_data.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* Totally Private Data Farm
Good news, renown advertising firm Evil Corp. wants to purchase our
private user data!
We'd never do this in real life of course, but just for practice
let's pretend we're unethical web hackers and transform the data
in the way Evil Corp. has requested. They're quite particular and
just want an array of users with a fullname and human readable
birthday.
Write a function that maps through the current data and returns
a new an array of objects with only two properties:
fullName and birthday. Each result in your
array should look like this when you're done:
{
fullName: "Levent Busser",
birthday: "Fri Aug 20 1971"
}
Read about toDateString() for info on formatting a readable date.
*/
const data = [
{
"name": {
"title": "Mr",
"first": "Levent",
"last": "Busser"
},
"dob": {
"date": "1971-08-21T01:08:00.099Z",
"age": 51
}
},
{
"name": {
"title": "Mr",
"first": "Kornelius",
"last": "Hamnes"
},
"dob": {
"date": "1961-09-23T13:13:49.283Z",
"age": 61
}
},
{
"name": {
"title": "Mademoiselle",
"first": "Ute",
"last": "Henry"
},
"dob": {
"date": "1956-06-30T11:33:42.549Z",
"age": 66
}
},
{
"name": {
"title": "Mr",
"first": "Estéfano",
"last": "Monteiro"
},
"dob": {
"date": "1945-07-16T19:48:22.796Z",
"age": 77
}
},
{
"name": {
"title": "Mr",
"first": "Oğuzhan",
"last": "Beşerler"
},
"dob": {
"date": "1947-09-28T10:12:30.102Z",
"age": 75
}
},
{
"name": {
"title": "Mrs",
"first": "Susanna",
"last": "Burke"
},
"dob": {
"date": "1961-06-13T21:41:24.455Z",
"age": 61
}
},
{
"name": {
"title": "Mrs",
"first": "Haritya",
"last": "Starickiy"
},
"dob": {
"date": "1945-12-14T21:29:22.822Z",
"age": 76
}
},
{
"name": {
"title": "Miss",
"first": "Nadja",
"last": "Branković"
},
"dob": {
"date": "1993-05-24T11:22:50.967Z",
"age": 29
}
},
{
"name": {
"title": "Ms",
"first": "Sonja",
"last": "Lenzen"
},
"dob": {
"date": "1945-03-21T04:33:04.759Z",
"age": 77
}
},
{
"name": {
"title": "Ms",
"first": "Shubhangi",
"last": "Chatterjee"
},
"dob": {
"date": "1956-12-25T18:50:29.484Z",
"age": 65
}
}
]
// Write a function that maps through the current data and returns
// a new an array of objects with only two properties:
// fullName , age and birthday.
function transformData(data) {
// use map to loop through the data
return data.map(({ name, dob }) => {
// return an object with the two new properties
// concat the first and last name
// create a new date object, passing in the dob
// format by calling toDateString() method
return {
fullName: `${name.first} ${name.last}`,
birthday: new Date(dob.date).toDateString(),
Age: `${dob.age}`
}
})
}
console.log(transformData(data));
// The toDateString() method returns a string representing the date portion of this date object, interpreted in the local timezone.