repl.it
@TheRealMrClean/

Convert Array To Object 3

Nodejs

No description

fork
loading
Files
  • index.js
index.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
function transformEmployeeData(employeeData) {

var employeeInfo = [];

// iterate through the first set of arrays
for (var i = 0; i < employeeData.length; i++){

	// employee object
	var employee = {};
	// var to hold employee array data
	var currentEmployee = employeeData[i];

		// iterate through employee array data
		for (var j = 0; j < currentEmployee.length; j++){
			// var to hold key
			var key = currentEmployee[j][0];
			// var to hold the value
			var value = currentEmployee[j][1];
			// add object at key equal to value
			employee[key] = value;
		}

	// push employee object to the employeeInfo array
	employeeInfo.push(employee);
	}

return employeeInfo;
}

var input = [
    [
        ['firstName', 'Joe'], ['lastName', 'Blow'], ['age', 42], ['role', 'clerk']
    ],
    [
        ['firstName', 'Mary'], ['lastName', 'Jenkins'], ['age', 36], ['role', 'manager']
    ]
];

output = transformEmployeeData(input);
console.log(output);