I am completely new to JavaScript, and taking a beginner level course at school.
I have an assignment in which we are asked to create a function that converts a 2D array into a dictionary, by using column headers as a key and data as a value for each row.
I think I know how to separate the first row (to use as headers) and the rest of the rows (to use as rows), but I am having a trouble converting this into a dictionary.
Here is what I have written so far:
function rowsToObjects(data) {
var headers = data[1];
data.shift();
//var rows = alert(data);
var rows = alert(data.slice(1));
}
And here is the example of what the output should look like:
const headers = ['a', 'b', 'c'];//Should not be hardcoded - subject to change depending on the grader
const rows = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];//Should not be hardcoded - subject to change depending on the grader
const result = rowsToObjects({headers, rows})
console.log(result);
// [{a: 1, b: 2, c: 3}, {a: 4, b: 5, c: 6}, {a: 7, b: 8, c: 9}];
I would have known how to create a dictionary for each row if I could use a for loop, but we are not allowed to use while loops, for loops, for ... in loops, for ... of loops, forEach method, so I am struggling with coming up with a way to do this, and make my output look like the ones that is shown in the example.
Any help is welcome, and thank you very much!