Skip to content

Instantly share code, notes, and snippets.

@mmontes11
Last active February 3, 2020 10:43
Show Gist options
  • Save mmontes11/a5a0a4fe7d4073e37776f5eac5fd8de2 to your computer and use it in GitHub Desktop.
Save mmontes11/a5a0a4fe7d4073e37776f5eac5fd8de2 to your computer and use it in GitHub Desktop.
Index an array by multiple fields
const indexBy = (value, ...fields) => {
if (fields.length === 0) {
return value;
}
const [currentField, ...rest] = fields;
const newValue = mapObjectValues(value, Array.isArray, indexByField, currentField);
return indexBy(newValue, ...rest);
};
Array.prototype.indexBy = function(...fields) {
return indexBy(this, ...fields);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment