I'm using an object's keys and values to populate other objects like so:
var default_columns = {
column_a: 'value_a',
column_b: 'value_b',
column_c: 'value_c'
// [...]
}
var new_object = {};
for (var key in default_columns) {
new_object[key] = default_columns[key];
}
But then later on in my program, I would like to resuse those keys as parameters. I could do something like this: new_object['column_a'] but if I change 'column_a' in default_columns I need to update it everywhere in the code.
I thought about defining my object like so:
var default_columns = {
a: { k: 'column_a', v: 'value_a' },
b: { k: 'column_b', v: 'value_b' },
c: { k: 'column_c', v: 'value_c' }
}
and iterate through it as follows:
var new_object = {};
for (var key in default_columns) {
new_object[default_columns[key].k] = default_columns[key].v;
}
which would also allow me to use the keys as parameters ( new_object[default_columns.a.k] ) while giving me the opportunity to change the keys (e.g. 'column_a' to 'my_column_a') in default_columns without having to update the code.
Is there a more readable way of doing what I'm trying to achieve with the 2nd approach?