Skip to content

Instantly share code, notes, and snippets.

@ahornerr
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahornerr/673f3cf640f564cba7a7 to your computer and use it in GitHub Desktop.
Save ahornerr/673f3cf640f564cba7a7 to your computer and use it in GitHub Desktop.
var columns = [
{id: 1234567890, title: "shittily named title"},
{id: 0987654321, title: "shitty title 2"}
];
var mapping_table = {
mapped_column1_name: "shitty title 2",
mapped_column2_name: "shittily named title"}
};
//This is the outcome I want
var column_id_map = {
mapped_column1_name: 0987654321,
mapped_column2_name: 1234567890
}
//Here's how I'm doing it in 3 lines
_.each(mapping_table, function (column_title, column_name) {
column_id_map[column_name] = _.result(_.find(columns, { 'title': column_title}), 'id');
});
//Slightly better with LoDash
sheet.column_ids = _.object(_.keys(sheet.column_names), _.map(sheet.column_names, function(title) {return _.result(_.find(columns, { 'title': title}), 'id');}));
/*
Using _.object, I can do _.object(_.keys(mapping_table), ARRAY_OF_VALUES)
to get something like
{
mapped_column1_name:
mapped_column2_name:
}
But I need to somehow apply the value from mapping_table to the columns array.
_.result(_.find(columns, { 'title': "shittily named title"}), 'id');
// → 1234567890
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment