-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHackReactor-Array_to_Object.js
More file actions
57 lines (48 loc) · 1.71 KB
/
HackReactor-Array_to_Object.js
File metadata and controls
57 lines (48 loc) · 1.71 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
46
47
48
49
50
51
52
53
54
55
56
57
/* This challenge came across the FreeCodeCamp / HelpJavaScript Room. I wanted to try it out.
I later found out that it was a challenge for Hack Reactor.
Write a function called "transformEmployeeData" that transforms some employee data from one format to another.
The argument will look something like this:
[
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
]
Given that input, the return value should look like this:
[
{firstName: 'Joe', lastName: 'Blow', age: 42, role: 'clerk'},
{firstName: 'Mary', lastName: 'Jenkins', age: 36, role: 'manager'}
]
Note that the input may have a different number of rows or different keys than the given sample.
For example, let's say the HR department adds a "tshirtSize" field to each employee record. Your code should flexibly accommodate that.
Starter Code :
function transformEmployeeData(array) {
// your code here
}
*/
function transformEmployeeData (array) {
var result = [];
for (var i = 0; i < array.length; i++) {
result.push(array[i].reduce(function(acc, cur) { acc[cur[0]] = cur[1]; return acc; }, {}));
}
return result;
}
var data1 = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
]
];
var data2 = [
[
['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk'], ["tshirtSize", "L"]
],
[
['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager'], ["tshirtSize", "M"]
]
];
transformEmployeeData(data1);