I'm writing a simple table-oriented storage for objects. The schema of a table is stored in the columns
-array and the data in the rows
-array.
// example: table "employees"
var columns = ["id", "name"]
var rows = [[1, "Alice"], [2, "Bob"]];
I wrote a function to convert a row-array into an object:
function convertRowToObject(row) {
var result = {};
for (var i = 0; i < columns.length; i++) result[columns[i]] = row[i];
return result;
}
Now I can get an employee-object where I can access the fields by name, not by index.
// [1, "Alice"] -> { id: 1, name: "Alice" }
Is there any way to get the convertRowToObject
function any smaller? I think there should be a way to get rid of the loop and make this with a single line of code.