Last active
February 3, 2020 10:43
-
-
Save mmontes11/a5a0a4fe7d4073e37776f5eac5fd8de2 to your computer and use it in GitHub Desktop.
Index an array by multiple fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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